xref: /openbmc/bmcweb/features/redfish/lib/log_services.hpp (revision 711ac7a931dd3f151fc4064063b5ea90404b9054)
11da66f75SEd Tanous /*
21da66f75SEd Tanous // Copyright (c) 2018 Intel Corporation
31da66f75SEd Tanous //
41da66f75SEd Tanous // Licensed under the Apache License, Version 2.0 (the "License");
51da66f75SEd Tanous // you may not use this file except in compliance with the License.
61da66f75SEd Tanous // You may obtain a copy of the License at
71da66f75SEd Tanous //
81da66f75SEd Tanous //      http://www.apache.org/licenses/LICENSE-2.0
91da66f75SEd Tanous //
101da66f75SEd Tanous // Unless required by applicable law or agreed to in writing, software
111da66f75SEd Tanous // distributed under the License is distributed on an "AS IS" BASIS,
121da66f75SEd Tanous // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
131da66f75SEd Tanous // See the License for the specific language governing permissions and
141da66f75SEd Tanous // limitations under the License.
151da66f75SEd Tanous */
161da66f75SEd Tanous #pragma once
171da66f75SEd Tanous 
18b7028ebfSSpencer Ku #include "gzfile.hpp"
19647b3cdcSGeorge Liu #include "http_utility.hpp"
20b7028ebfSSpencer Ku #include "human_sort.hpp"
214851d45dSJason M. Bills #include "registries.hpp"
224851d45dSJason M. Bills #include "registries/base_message_registry.hpp"
234851d45dSJason M. Bills #include "registries/openbmc_message_registry.hpp"
2446229577SJames Feist #include "task.hpp"
251da66f75SEd Tanous 
26e1f26343SJason M. Bills #include <systemd/sd-journal.h>
27400fd1fbSAdriana Kobylak #include <unistd.h>
28e1f26343SJason M. Bills 
297e860f15SJohn Edward Broadbent #include <app.hpp>
30400fd1fbSAdriana Kobylak #include <boost/algorithm/string/replace.hpp>
314851d45dSJason M. Bills #include <boost/algorithm/string/split.hpp>
32400fd1fbSAdriana Kobylak #include <boost/beast/http.hpp>
331da66f75SEd Tanous #include <boost/container/flat_map.hpp>
341ddcf01aSJason M. Bills #include <boost/system/linux_error.hpp>
35168e20c1SEd Tanous #include <dbus_utility.hpp>
36cb92c03bSAndrew Geissler #include <error_messages.hpp>
37ed398213SEd Tanous #include <registries/privilege_registry.hpp>
381214b7e7SGunnar Mills 
39647b3cdcSGeorge Liu #include <charconv>
404418c7f0SJames Feist #include <filesystem>
4175710de2SXiaochao Ma #include <optional>
4226702d01SEd Tanous #include <span>
43cd225da8SJason M. Bills #include <string_view>
44abf2add6SEd Tanous #include <variant>
451da66f75SEd Tanous 
461da66f75SEd Tanous namespace redfish
471da66f75SEd Tanous {
481da66f75SEd Tanous 
495b61b5e8SJason M. Bills constexpr char const* crashdumpObject = "com.intel.crashdump";
505b61b5e8SJason M. Bills constexpr char const* crashdumpPath = "/com/intel/crashdump";
515b61b5e8SJason M. Bills constexpr char const* crashdumpInterface = "com.intel.crashdump";
525b61b5e8SJason M. Bills constexpr char const* deleteAllInterface =
535b61b5e8SJason M. Bills     "xyz.openbmc_project.Collection.DeleteAll";
545b61b5e8SJason M. Bills constexpr char const* crashdumpOnDemandInterface =
55424c4176SJason M. Bills     "com.intel.crashdump.OnDemand";
566eda7685SKenny L. Ku constexpr char const* crashdumpTelemetryInterface =
576eda7685SKenny L. Ku     "com.intel.crashdump.Telemetry";
581da66f75SEd Tanous 
594851d45dSJason M. Bills namespace message_registries
604851d45dSJason M. Bills {
6126702d01SEd Tanous static const Message*
6226702d01SEd Tanous     getMessageFromRegistry(const std::string& messageKey,
6326702d01SEd Tanous                            const std::span<const MessageEntry> registry)
644851d45dSJason M. Bills {
6526702d01SEd Tanous     std::span<const MessageEntry>::iterator messageIt = std::find_if(
6626702d01SEd Tanous         registry.begin(), registry.end(),
674851d45dSJason M. Bills         [&messageKey](const MessageEntry& messageEntry) {
6826702d01SEd Tanous             return !std::strcmp(messageEntry.first, messageKey.c_str());
694851d45dSJason M. Bills         });
7026702d01SEd Tanous     if (messageIt != registry.end())
714851d45dSJason M. Bills     {
724851d45dSJason M. Bills         return &messageIt->second;
734851d45dSJason M. Bills     }
744851d45dSJason M. Bills 
754851d45dSJason M. Bills     return nullptr;
764851d45dSJason M. Bills }
774851d45dSJason M. Bills 
784851d45dSJason M. Bills static const Message* getMessage(const std::string_view& messageID)
794851d45dSJason M. Bills {
804851d45dSJason M. Bills     // Redfish MessageIds are in the form
814851d45dSJason M. Bills     // RegistryName.MajorVersion.MinorVersion.MessageKey, so parse it to find
824851d45dSJason M. Bills     // the right Message
834851d45dSJason M. Bills     std::vector<std::string> fields;
844851d45dSJason M. Bills     fields.reserve(4);
854851d45dSJason M. Bills     boost::split(fields, messageID, boost::is_any_of("."));
864851d45dSJason M. Bills     std::string& registryName = fields[0];
874851d45dSJason M. Bills     std::string& messageKey = fields[3];
884851d45dSJason M. Bills 
894851d45dSJason M. Bills     // Find the right registry and check it for the MessageKey
904851d45dSJason M. Bills     if (std::string(base::header.registryPrefix) == registryName)
914851d45dSJason M. Bills     {
924851d45dSJason M. Bills         return getMessageFromRegistry(
9326702d01SEd Tanous             messageKey, std::span<const MessageEntry>(base::registry));
944851d45dSJason M. Bills     }
954851d45dSJason M. Bills     if (std::string(openbmc::header.registryPrefix) == registryName)
964851d45dSJason M. Bills     {
974851d45dSJason M. Bills         return getMessageFromRegistry(
9826702d01SEd Tanous             messageKey, std::span<const MessageEntry>(openbmc::registry));
994851d45dSJason M. Bills     }
1004851d45dSJason M. Bills     return nullptr;
1014851d45dSJason M. Bills }
1024851d45dSJason M. Bills } // namespace message_registries
1034851d45dSJason M. Bills 
104f6150403SJames Feist namespace fs = std::filesystem;
1051da66f75SEd Tanous 
106168e20c1SEd Tanous using GetManagedPropertyType =
107168e20c1SEd Tanous     boost::container::flat_map<std::string, dbus::utility::DbusVariantType>;
108cb92c03bSAndrew Geissler 
109cb92c03bSAndrew Geissler inline std::string translateSeverityDbusToRedfish(const std::string& s)
110cb92c03bSAndrew Geissler {
111d4d25793SEd Tanous     if ((s == "xyz.openbmc_project.Logging.Entry.Level.Alert") ||
112d4d25793SEd Tanous         (s == "xyz.openbmc_project.Logging.Entry.Level.Critical") ||
113d4d25793SEd Tanous         (s == "xyz.openbmc_project.Logging.Entry.Level.Emergency") ||
114d4d25793SEd Tanous         (s == "xyz.openbmc_project.Logging.Entry.Level.Error"))
115cb92c03bSAndrew Geissler     {
116cb92c03bSAndrew Geissler         return "Critical";
117cb92c03bSAndrew Geissler     }
1183174e4dfSEd Tanous     if ((s == "xyz.openbmc_project.Logging.Entry.Level.Debug") ||
119d4d25793SEd Tanous         (s == "xyz.openbmc_project.Logging.Entry.Level.Informational") ||
120d4d25793SEd Tanous         (s == "xyz.openbmc_project.Logging.Entry.Level.Notice"))
121cb92c03bSAndrew Geissler     {
122cb92c03bSAndrew Geissler         return "OK";
123cb92c03bSAndrew Geissler     }
1243174e4dfSEd Tanous     if (s == "xyz.openbmc_project.Logging.Entry.Level.Warning")
125cb92c03bSAndrew Geissler     {
126cb92c03bSAndrew Geissler         return "Warning";
127cb92c03bSAndrew Geissler     }
128cb92c03bSAndrew Geissler     return "";
129cb92c03bSAndrew Geissler }
130cb92c03bSAndrew Geissler 
1317e860f15SJohn Edward Broadbent inline static int getJournalMetadata(sd_journal* journal,
13239e77504SEd Tanous                                      const std::string_view& field,
13339e77504SEd Tanous                                      std::string_view& contents)
13416428a1aSJason M. Bills {
13516428a1aSJason M. Bills     const char* data = nullptr;
13616428a1aSJason M. Bills     size_t length = 0;
13716428a1aSJason M. Bills     int ret = 0;
13816428a1aSJason M. Bills     // Get the metadata from the requested field of the journal entry
139271584abSEd Tanous     ret = sd_journal_get_data(journal, field.data(),
140271584abSEd Tanous                               reinterpret_cast<const void**>(&data), &length);
14116428a1aSJason M. Bills     if (ret < 0)
14216428a1aSJason M. Bills     {
14316428a1aSJason M. Bills         return ret;
14416428a1aSJason M. Bills     }
14539e77504SEd Tanous     contents = std::string_view(data, length);
14616428a1aSJason M. Bills     // Only use the content after the "=" character.
14781ce609eSEd Tanous     contents.remove_prefix(std::min(contents.find('=') + 1, contents.size()));
14816428a1aSJason M. Bills     return ret;
14916428a1aSJason M. Bills }
15016428a1aSJason M. Bills 
1517e860f15SJohn Edward Broadbent inline static int getJournalMetadata(sd_journal* journal,
1527e860f15SJohn Edward Broadbent                                      const std::string_view& field,
1537e860f15SJohn Edward Broadbent                                      const int& base, long int& contents)
15416428a1aSJason M. Bills {
15516428a1aSJason M. Bills     int ret = 0;
15639e77504SEd Tanous     std::string_view metadata;
15716428a1aSJason M. Bills     // Get the metadata from the requested field of the journal entry
15816428a1aSJason M. Bills     ret = getJournalMetadata(journal, field, metadata);
15916428a1aSJason M. Bills     if (ret < 0)
16016428a1aSJason M. Bills     {
16116428a1aSJason M. Bills         return ret;
16216428a1aSJason M. Bills     }
163b01bf299SEd Tanous     contents = strtol(metadata.data(), nullptr, base);
16416428a1aSJason M. Bills     return ret;
16516428a1aSJason M. Bills }
16616428a1aSJason M. Bills 
1677e860f15SJohn Edward Broadbent inline static bool getEntryTimestamp(sd_journal* journal,
1687e860f15SJohn Edward Broadbent                                      std::string& entryTimestamp)
169a3316fc6SZhikuiRen {
170a3316fc6SZhikuiRen     int ret = 0;
171a3316fc6SZhikuiRen     uint64_t timestamp = 0;
172a3316fc6SZhikuiRen     ret = sd_journal_get_realtime_usec(journal, &timestamp);
173a3316fc6SZhikuiRen     if (ret < 0)
174a3316fc6SZhikuiRen     {
175a3316fc6SZhikuiRen         BMCWEB_LOG_ERROR << "Failed to read entry timestamp: "
176a3316fc6SZhikuiRen                          << strerror(-ret);
177a3316fc6SZhikuiRen         return false;
178a3316fc6SZhikuiRen     }
1791d8782e7SNan Zhou     entryTimestamp = crow::utility::getDateTimeUint(timestamp / 1000 / 1000);
1809c620e21SAsmitha Karunanithi     return true;
181a3316fc6SZhikuiRen }
182a3316fc6SZhikuiRen 
1838d1b46d7Szhanghch05 static bool getSkipParam(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
1848d1b46d7Szhanghch05                          const crow::Request& req, uint64_t& skip)
18516428a1aSJason M. Bills {
186d32c4fa9SEd Tanous     boost::urls::query_params_view::iterator it = req.urlParams.find("$skip");
1875a7e877eSJames Feist     if (it != req.urlParams.end())
18816428a1aSJason M. Bills     {
1895a7e877eSJames Feist         std::string skipParam = it->value();
19016428a1aSJason M. Bills         char* ptr = nullptr;
1915a7e877eSJames Feist         skip = std::strtoul(skipParam.c_str(), &ptr, 10);
1925a7e877eSJames Feist         if (skipParam.empty() || *ptr != '\0')
19316428a1aSJason M. Bills         {
19416428a1aSJason M. Bills 
1958d1b46d7Szhanghch05             messages::queryParameterValueTypeError(
1968d1b46d7Szhanghch05                 asyncResp->res, std::string(skipParam), "$skip");
19716428a1aSJason M. Bills             return false;
19816428a1aSJason M. Bills         }
19916428a1aSJason M. Bills     }
20016428a1aSJason M. Bills     return true;
20116428a1aSJason M. Bills }
20216428a1aSJason M. Bills 
203271584abSEd Tanous static constexpr const uint64_t maxEntriesPerPage = 1000;
2048d1b46d7Szhanghch05 static bool getTopParam(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
2058d1b46d7Szhanghch05                         const crow::Request& req, uint64_t& top)
20616428a1aSJason M. Bills {
207d32c4fa9SEd Tanous     boost::urls::query_params_view::iterator it = req.urlParams.find("$top");
2085a7e877eSJames Feist     if (it != req.urlParams.end())
20916428a1aSJason M. Bills     {
2105a7e877eSJames Feist         std::string topParam = it->value();
21116428a1aSJason M. Bills         char* ptr = nullptr;
2125a7e877eSJames Feist         top = std::strtoul(topParam.c_str(), &ptr, 10);
2135a7e877eSJames Feist         if (topParam.empty() || *ptr != '\0')
21416428a1aSJason M. Bills         {
2158d1b46d7Szhanghch05             messages::queryParameterValueTypeError(
2168d1b46d7Szhanghch05                 asyncResp->res, std::string(topParam), "$top");
21716428a1aSJason M. Bills             return false;
21816428a1aSJason M. Bills         }
219271584abSEd Tanous         if (top < 1U || top > maxEntriesPerPage)
22016428a1aSJason M. Bills         {
22116428a1aSJason M. Bills 
22216428a1aSJason M. Bills             messages::queryParameterOutOfRange(
2238d1b46d7Szhanghch05                 asyncResp->res, std::to_string(top), "$top",
22416428a1aSJason M. Bills                 "1-" + std::to_string(maxEntriesPerPage));
22516428a1aSJason M. Bills             return false;
22616428a1aSJason M. Bills         }
22716428a1aSJason M. Bills     }
22816428a1aSJason M. Bills     return true;
22916428a1aSJason M. Bills }
23016428a1aSJason M. Bills 
2317e860f15SJohn Edward Broadbent inline static bool getUniqueEntryID(sd_journal* journal, std::string& entryID,
232e85d6b16SJason M. Bills                                     const bool firstEntry = true)
23316428a1aSJason M. Bills {
23416428a1aSJason M. Bills     int ret = 0;
23516428a1aSJason M. Bills     static uint64_t prevTs = 0;
23616428a1aSJason M. Bills     static int index = 0;
237e85d6b16SJason M. Bills     if (firstEntry)
238e85d6b16SJason M. Bills     {
239e85d6b16SJason M. Bills         prevTs = 0;
240e85d6b16SJason M. Bills     }
241e85d6b16SJason M. Bills 
24216428a1aSJason M. Bills     // Get the entry timestamp
24316428a1aSJason M. Bills     uint64_t curTs = 0;
24416428a1aSJason M. Bills     ret = sd_journal_get_realtime_usec(journal, &curTs);
24516428a1aSJason M. Bills     if (ret < 0)
24616428a1aSJason M. Bills     {
24716428a1aSJason M. Bills         BMCWEB_LOG_ERROR << "Failed to read entry timestamp: "
24816428a1aSJason M. Bills                          << strerror(-ret);
24916428a1aSJason M. Bills         return false;
25016428a1aSJason M. Bills     }
25116428a1aSJason M. Bills     // If the timestamp isn't unique, increment the index
25216428a1aSJason M. Bills     if (curTs == prevTs)
25316428a1aSJason M. Bills     {
25416428a1aSJason M. Bills         index++;
25516428a1aSJason M. Bills     }
25616428a1aSJason M. Bills     else
25716428a1aSJason M. Bills     {
25816428a1aSJason M. Bills         // Otherwise, reset it
25916428a1aSJason M. Bills         index = 0;
26016428a1aSJason M. Bills     }
26116428a1aSJason M. Bills     // Save the timestamp
26216428a1aSJason M. Bills     prevTs = curTs;
26316428a1aSJason M. Bills 
26416428a1aSJason M. Bills     entryID = std::to_string(curTs);
26516428a1aSJason M. Bills     if (index > 0)
26616428a1aSJason M. Bills     {
26716428a1aSJason M. Bills         entryID += "_" + std::to_string(index);
26816428a1aSJason M. Bills     }
26916428a1aSJason M. Bills     return true;
27016428a1aSJason M. Bills }
27116428a1aSJason M. Bills 
272e85d6b16SJason M. Bills static bool getUniqueEntryID(const std::string& logEntry, std::string& entryID,
273e85d6b16SJason M. Bills                              const bool firstEntry = true)
27495820184SJason M. Bills {
275271584abSEd Tanous     static time_t prevTs = 0;
27695820184SJason M. Bills     static int index = 0;
277e85d6b16SJason M. Bills     if (firstEntry)
278e85d6b16SJason M. Bills     {
279e85d6b16SJason M. Bills         prevTs = 0;
280e85d6b16SJason M. Bills     }
281e85d6b16SJason M. Bills 
28295820184SJason M. Bills     // Get the entry timestamp
283271584abSEd Tanous     std::time_t curTs = 0;
28495820184SJason M. Bills     std::tm timeStruct = {};
28595820184SJason M. Bills     std::istringstream entryStream(logEntry);
28695820184SJason M. Bills     if (entryStream >> std::get_time(&timeStruct, "%Y-%m-%dT%H:%M:%S"))
28795820184SJason M. Bills     {
28895820184SJason M. Bills         curTs = std::mktime(&timeStruct);
28995820184SJason M. Bills     }
29095820184SJason M. Bills     // If the timestamp isn't unique, increment the index
29195820184SJason M. Bills     if (curTs == prevTs)
29295820184SJason M. Bills     {
29395820184SJason M. Bills         index++;
29495820184SJason M. Bills     }
29595820184SJason M. Bills     else
29695820184SJason M. Bills     {
29795820184SJason M. Bills         // Otherwise, reset it
29895820184SJason M. Bills         index = 0;
29995820184SJason M. Bills     }
30095820184SJason M. Bills     // Save the timestamp
30195820184SJason M. Bills     prevTs = curTs;
30295820184SJason M. Bills 
30395820184SJason M. Bills     entryID = std::to_string(curTs);
30495820184SJason M. Bills     if (index > 0)
30595820184SJason M. Bills     {
30695820184SJason M. Bills         entryID += "_" + std::to_string(index);
30795820184SJason M. Bills     }
30895820184SJason M. Bills     return true;
30995820184SJason M. Bills }
31095820184SJason M. Bills 
3117e860f15SJohn Edward Broadbent inline static bool
3128d1b46d7Szhanghch05     getTimestampFromID(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
3138d1b46d7Szhanghch05                        const std::string& entryID, uint64_t& timestamp,
3148d1b46d7Szhanghch05                        uint64_t& index)
31516428a1aSJason M. Bills {
31616428a1aSJason M. Bills     if (entryID.empty())
31716428a1aSJason M. Bills     {
31816428a1aSJason M. Bills         return false;
31916428a1aSJason M. Bills     }
32016428a1aSJason M. Bills     // Convert the unique ID back to a timestamp to find the entry
32139e77504SEd Tanous     std::string_view tsStr(entryID);
32216428a1aSJason M. Bills 
32381ce609eSEd Tanous     auto underscorePos = tsStr.find('_');
32416428a1aSJason M. Bills     if (underscorePos != tsStr.npos)
32516428a1aSJason M. Bills     {
32616428a1aSJason M. Bills         // Timestamp has an index
32716428a1aSJason M. Bills         tsStr.remove_suffix(tsStr.size() - underscorePos);
32839e77504SEd Tanous         std::string_view indexStr(entryID);
32916428a1aSJason M. Bills         indexStr.remove_prefix(underscorePos + 1);
330c0bd5e4bSEd Tanous         auto [ptr, ec] = std::from_chars(
331c0bd5e4bSEd Tanous             indexStr.data(), indexStr.data() + indexStr.size(), index);
332c0bd5e4bSEd Tanous         if (ec != std::errc())
33316428a1aSJason M. Bills         {
3348d1b46d7Szhanghch05             messages::resourceMissingAtURI(asyncResp->res, entryID);
33516428a1aSJason M. Bills             return false;
33616428a1aSJason M. Bills         }
33716428a1aSJason M. Bills     }
33816428a1aSJason M. Bills     // Timestamp has no index
339c0bd5e4bSEd Tanous     auto [ptr, ec] =
340c0bd5e4bSEd Tanous         std::from_chars(tsStr.data(), tsStr.data() + tsStr.size(), timestamp);
341c0bd5e4bSEd Tanous     if (ec != std::errc())
34216428a1aSJason M. Bills     {
3438d1b46d7Szhanghch05         messages::resourceMissingAtURI(asyncResp->res, entryID);
34416428a1aSJason M. Bills         return false;
34516428a1aSJason M. Bills     }
34616428a1aSJason M. Bills     return true;
34716428a1aSJason M. Bills }
34816428a1aSJason M. Bills 
34995820184SJason M. Bills static bool
35095820184SJason M. Bills     getRedfishLogFiles(std::vector<std::filesystem::path>& redfishLogFiles)
35195820184SJason M. Bills {
35295820184SJason M. Bills     static const std::filesystem::path redfishLogDir = "/var/log";
35395820184SJason M. Bills     static const std::string redfishLogFilename = "redfish";
35495820184SJason M. Bills 
35595820184SJason M. Bills     // Loop through the directory looking for redfish log files
35695820184SJason M. Bills     for (const std::filesystem::directory_entry& dirEnt :
35795820184SJason M. Bills          std::filesystem::directory_iterator(redfishLogDir))
35895820184SJason M. Bills     {
35995820184SJason M. Bills         // If we find a redfish log file, save the path
36095820184SJason M. Bills         std::string filename = dirEnt.path().filename();
36195820184SJason M. Bills         if (boost::starts_with(filename, redfishLogFilename))
36295820184SJason M. Bills         {
36395820184SJason M. Bills             redfishLogFiles.emplace_back(redfishLogDir / filename);
36495820184SJason M. Bills         }
36595820184SJason M. Bills     }
36695820184SJason M. Bills     // As the log files rotate, they are appended with a ".#" that is higher for
36795820184SJason M. Bills     // the older logs. Since we don't expect more than 10 log files, we
36895820184SJason M. Bills     // can just sort the list to get them in order from newest to oldest
36995820184SJason M. Bills     std::sort(redfishLogFiles.begin(), redfishLogFiles.end());
37095820184SJason M. Bills 
37195820184SJason M. Bills     return !redfishLogFiles.empty();
37295820184SJason M. Bills }
37395820184SJason M. Bills 
3748d1b46d7Szhanghch05 inline void
3758d1b46d7Szhanghch05     getDumpEntryCollection(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
3765cb1dd27SAsmitha Karunanithi                            const std::string& dumpType)
3775cb1dd27SAsmitha Karunanithi {
3785cb1dd27SAsmitha Karunanithi     std::string dumpPath;
3795cb1dd27SAsmitha Karunanithi     if (dumpType == "BMC")
3805cb1dd27SAsmitha Karunanithi     {
3815cb1dd27SAsmitha Karunanithi         dumpPath = "/redfish/v1/Managers/bmc/LogServices/Dump/Entries/";
3825cb1dd27SAsmitha Karunanithi     }
3835cb1dd27SAsmitha Karunanithi     else if (dumpType == "System")
3845cb1dd27SAsmitha Karunanithi     {
3855cb1dd27SAsmitha Karunanithi         dumpPath = "/redfish/v1/Systems/system/LogServices/Dump/Entries/";
3865cb1dd27SAsmitha Karunanithi     }
3875cb1dd27SAsmitha Karunanithi     else
3885cb1dd27SAsmitha Karunanithi     {
3895cb1dd27SAsmitha Karunanithi         BMCWEB_LOG_ERROR << "Invalid dump type" << dumpType;
3905cb1dd27SAsmitha Karunanithi         messages::internalError(asyncResp->res);
3915cb1dd27SAsmitha Karunanithi         return;
3925cb1dd27SAsmitha Karunanithi     }
3935cb1dd27SAsmitha Karunanithi 
3945cb1dd27SAsmitha Karunanithi     crow::connections::systemBus->async_method_call(
395*711ac7a9SEd Tanous         [asyncResp, dumpPath,
396*711ac7a9SEd Tanous          dumpType](const boost::system::error_code ec,
397*711ac7a9SEd Tanous                    dbus::utility::ManagedObjectType& resp) {
3985cb1dd27SAsmitha Karunanithi             if (ec)
3995cb1dd27SAsmitha Karunanithi             {
4005cb1dd27SAsmitha Karunanithi                 BMCWEB_LOG_ERROR << "DumpEntry resp_handler got error " << ec;
4015cb1dd27SAsmitha Karunanithi                 messages::internalError(asyncResp->res);
4025cb1dd27SAsmitha Karunanithi                 return;
4035cb1dd27SAsmitha Karunanithi             }
4045cb1dd27SAsmitha Karunanithi 
4055cb1dd27SAsmitha Karunanithi             nlohmann::json& entriesArray = asyncResp->res.jsonValue["Members"];
4065cb1dd27SAsmitha Karunanithi             entriesArray = nlohmann::json::array();
407b47452b2SAsmitha Karunanithi             std::string dumpEntryPath =
408b47452b2SAsmitha Karunanithi                 "/xyz/openbmc_project/dump/" +
409b47452b2SAsmitha Karunanithi                 std::string(boost::algorithm::to_lower_copy(dumpType)) +
410b47452b2SAsmitha Karunanithi                 "/entry/";
4115cb1dd27SAsmitha Karunanithi 
4125cb1dd27SAsmitha Karunanithi             for (auto& object : resp)
4135cb1dd27SAsmitha Karunanithi             {
414b47452b2SAsmitha Karunanithi                 if (object.first.str.find(dumpEntryPath) == std::string::npos)
4155cb1dd27SAsmitha Karunanithi                 {
4165cb1dd27SAsmitha Karunanithi                     continue;
4175cb1dd27SAsmitha Karunanithi                 }
4181d8782e7SNan Zhou                 uint64_t timestamp = 0;
4195cb1dd27SAsmitha Karunanithi                 uint64_t size = 0;
42035440d18SAsmitha Karunanithi                 std::string dumpStatus;
42135440d18SAsmitha Karunanithi                 nlohmann::json thisEntry;
4222dfd18efSEd Tanous 
4232dfd18efSEd Tanous                 std::string entryID = object.first.filename();
4242dfd18efSEd Tanous                 if (entryID.empty())
4255cb1dd27SAsmitha Karunanithi                 {
4265cb1dd27SAsmitha Karunanithi                     continue;
4275cb1dd27SAsmitha Karunanithi                 }
4285cb1dd27SAsmitha Karunanithi 
4295cb1dd27SAsmitha Karunanithi                 for (auto& interfaceMap : object.second)
4305cb1dd27SAsmitha Karunanithi                 {
43135440d18SAsmitha Karunanithi                     if (interfaceMap.first ==
43235440d18SAsmitha Karunanithi                         "xyz.openbmc_project.Common.Progress")
43335440d18SAsmitha Karunanithi                     {
43435440d18SAsmitha Karunanithi                         for (auto& propertyMap : interfaceMap.second)
43535440d18SAsmitha Karunanithi                         {
43635440d18SAsmitha Karunanithi                             if (propertyMap.first == "Status")
43735440d18SAsmitha Karunanithi                             {
43835440d18SAsmitha Karunanithi                                 auto status = std::get_if<std::string>(
43935440d18SAsmitha Karunanithi                                     &propertyMap.second);
44035440d18SAsmitha Karunanithi                                 if (status == nullptr)
44135440d18SAsmitha Karunanithi                                 {
44235440d18SAsmitha Karunanithi                                     messages::internalError(asyncResp->res);
44335440d18SAsmitha Karunanithi                                     break;
44435440d18SAsmitha Karunanithi                                 }
44535440d18SAsmitha Karunanithi                                 dumpStatus = *status;
44635440d18SAsmitha Karunanithi                             }
44735440d18SAsmitha Karunanithi                         }
44835440d18SAsmitha Karunanithi                     }
44935440d18SAsmitha Karunanithi                     else if (interfaceMap.first ==
45035440d18SAsmitha Karunanithi                              "xyz.openbmc_project.Dump.Entry")
4515cb1dd27SAsmitha Karunanithi                     {
4525cb1dd27SAsmitha Karunanithi 
4535cb1dd27SAsmitha Karunanithi                         for (auto& propertyMap : interfaceMap.second)
4545cb1dd27SAsmitha Karunanithi                         {
4555cb1dd27SAsmitha Karunanithi                             if (propertyMap.first == "Size")
4565cb1dd27SAsmitha Karunanithi                             {
4575cb1dd27SAsmitha Karunanithi                                 auto sizePtr =
4585cb1dd27SAsmitha Karunanithi                                     std::get_if<uint64_t>(&propertyMap.second);
4595cb1dd27SAsmitha Karunanithi                                 if (sizePtr == nullptr)
4605cb1dd27SAsmitha Karunanithi                                 {
4615cb1dd27SAsmitha Karunanithi                                     messages::internalError(asyncResp->res);
4625cb1dd27SAsmitha Karunanithi                                     break;
4635cb1dd27SAsmitha Karunanithi                                 }
4645cb1dd27SAsmitha Karunanithi                                 size = *sizePtr;
4655cb1dd27SAsmitha Karunanithi                                 break;
4665cb1dd27SAsmitha Karunanithi                             }
4675cb1dd27SAsmitha Karunanithi                         }
4685cb1dd27SAsmitha Karunanithi                     }
4695cb1dd27SAsmitha Karunanithi                     else if (interfaceMap.first ==
4705cb1dd27SAsmitha Karunanithi                              "xyz.openbmc_project.Time.EpochTime")
4715cb1dd27SAsmitha Karunanithi                     {
4725cb1dd27SAsmitha Karunanithi 
4735cb1dd27SAsmitha Karunanithi                         for (auto& propertyMap : interfaceMap.second)
4745cb1dd27SAsmitha Karunanithi                         {
4755cb1dd27SAsmitha Karunanithi                             if (propertyMap.first == "Elapsed")
4765cb1dd27SAsmitha Karunanithi                             {
4775cb1dd27SAsmitha Karunanithi                                 const uint64_t* usecsTimeStamp =
4785cb1dd27SAsmitha Karunanithi                                     std::get_if<uint64_t>(&propertyMap.second);
4795cb1dd27SAsmitha Karunanithi                                 if (usecsTimeStamp == nullptr)
4805cb1dd27SAsmitha Karunanithi                                 {
4815cb1dd27SAsmitha Karunanithi                                     messages::internalError(asyncResp->res);
4825cb1dd27SAsmitha Karunanithi                                     break;
4835cb1dd27SAsmitha Karunanithi                                 }
4841d8782e7SNan Zhou                                 timestamp = (*usecsTimeStamp / 1000 / 1000);
4855cb1dd27SAsmitha Karunanithi                                 break;
4865cb1dd27SAsmitha Karunanithi                             }
4875cb1dd27SAsmitha Karunanithi                         }
4885cb1dd27SAsmitha Karunanithi                     }
4895cb1dd27SAsmitha Karunanithi                 }
4905cb1dd27SAsmitha Karunanithi 
4910fda0f12SGeorge Liu                 if (dumpStatus !=
4920fda0f12SGeorge Liu                         "xyz.openbmc_project.Common.Progress.OperationStatus.Completed" &&
49335440d18SAsmitha Karunanithi                     !dumpStatus.empty())
49435440d18SAsmitha Karunanithi                 {
49535440d18SAsmitha Karunanithi                     // Dump status is not Complete, no need to enumerate
49635440d18SAsmitha Karunanithi                     continue;
49735440d18SAsmitha Karunanithi                 }
49835440d18SAsmitha Karunanithi 
499647b3cdcSGeorge Liu                 thisEntry["@odata.type"] = "#LogEntry.v1_8_0.LogEntry";
5005cb1dd27SAsmitha Karunanithi                 thisEntry["@odata.id"] = dumpPath + entryID;
5015cb1dd27SAsmitha Karunanithi                 thisEntry["Id"] = entryID;
5025cb1dd27SAsmitha Karunanithi                 thisEntry["EntryType"] = "Event";
5031d8782e7SNan Zhou                 thisEntry["Created"] =
5041d8782e7SNan Zhou                     crow::utility::getDateTimeUint(timestamp);
5055cb1dd27SAsmitha Karunanithi                 thisEntry["Name"] = dumpType + " Dump Entry";
5065cb1dd27SAsmitha Karunanithi 
507d337bb72SAsmitha Karunanithi                 thisEntry["AdditionalDataSizeBytes"] = size;
5085cb1dd27SAsmitha Karunanithi 
5095cb1dd27SAsmitha Karunanithi                 if (dumpType == "BMC")
5105cb1dd27SAsmitha Karunanithi                 {
511d337bb72SAsmitha Karunanithi                     thisEntry["DiagnosticDataType"] = "Manager";
512d337bb72SAsmitha Karunanithi                     thisEntry["AdditionalDataURI"] =
513de8d94a3SAbhishek Patel                         "/redfish/v1/Managers/bmc/LogServices/Dump/Entries/" +
514de8d94a3SAbhishek Patel                         entryID + "/attachment";
5155cb1dd27SAsmitha Karunanithi                 }
5165cb1dd27SAsmitha Karunanithi                 else if (dumpType == "System")
5175cb1dd27SAsmitha Karunanithi                 {
518d337bb72SAsmitha Karunanithi                     thisEntry["DiagnosticDataType"] = "OEM";
519d337bb72SAsmitha Karunanithi                     thisEntry["OEMDiagnosticDataType"] = "System";
520d337bb72SAsmitha Karunanithi                     thisEntry["AdditionalDataURI"] =
521de8d94a3SAbhishek Patel                         "/redfish/v1/Systems/system/LogServices/Dump/Entries/" +
522de8d94a3SAbhishek Patel                         entryID + "/attachment";
5235cb1dd27SAsmitha Karunanithi                 }
52435440d18SAsmitha Karunanithi                 entriesArray.push_back(std::move(thisEntry));
5255cb1dd27SAsmitha Karunanithi             }
5265cb1dd27SAsmitha Karunanithi             asyncResp->res.jsonValue["Members@odata.count"] =
5275cb1dd27SAsmitha Karunanithi                 entriesArray.size();
5285cb1dd27SAsmitha Karunanithi         },
5295cb1dd27SAsmitha Karunanithi         "xyz.openbmc_project.Dump.Manager", "/xyz/openbmc_project/dump",
5305cb1dd27SAsmitha Karunanithi         "org.freedesktop.DBus.ObjectManager", "GetManagedObjects");
5315cb1dd27SAsmitha Karunanithi }
5325cb1dd27SAsmitha Karunanithi 
5338d1b46d7Szhanghch05 inline void
5348d1b46d7Szhanghch05     getDumpEntryById(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
5358d1b46d7Szhanghch05                      const std::string& entryID, const std::string& dumpType)
5365cb1dd27SAsmitha Karunanithi {
5375cb1dd27SAsmitha Karunanithi     std::string dumpPath;
5385cb1dd27SAsmitha Karunanithi     if (dumpType == "BMC")
5395cb1dd27SAsmitha Karunanithi     {
5405cb1dd27SAsmitha Karunanithi         dumpPath = "/redfish/v1/Managers/bmc/LogServices/Dump/Entries/";
5415cb1dd27SAsmitha Karunanithi     }
5425cb1dd27SAsmitha Karunanithi     else if (dumpType == "System")
5435cb1dd27SAsmitha Karunanithi     {
5445cb1dd27SAsmitha Karunanithi         dumpPath = "/redfish/v1/Systems/system/LogServices/Dump/Entries/";
5455cb1dd27SAsmitha Karunanithi     }
5465cb1dd27SAsmitha Karunanithi     else
5475cb1dd27SAsmitha Karunanithi     {
5485cb1dd27SAsmitha Karunanithi         BMCWEB_LOG_ERROR << "Invalid dump type" << dumpType;
5495cb1dd27SAsmitha Karunanithi         messages::internalError(asyncResp->res);
5505cb1dd27SAsmitha Karunanithi         return;
5515cb1dd27SAsmitha Karunanithi     }
5525cb1dd27SAsmitha Karunanithi 
5535cb1dd27SAsmitha Karunanithi     crow::connections::systemBus->async_method_call(
554*711ac7a9SEd Tanous         [asyncResp, entryID, dumpPath,
555*711ac7a9SEd Tanous          dumpType](const boost::system::error_code ec,
556*711ac7a9SEd Tanous                    dbus::utility::ManagedObjectType& resp) {
5575cb1dd27SAsmitha Karunanithi             if (ec)
5585cb1dd27SAsmitha Karunanithi             {
5595cb1dd27SAsmitha Karunanithi                 BMCWEB_LOG_ERROR << "DumpEntry resp_handler got error " << ec;
5605cb1dd27SAsmitha Karunanithi                 messages::internalError(asyncResp->res);
5615cb1dd27SAsmitha Karunanithi                 return;
5625cb1dd27SAsmitha Karunanithi             }
5635cb1dd27SAsmitha Karunanithi 
564b47452b2SAsmitha Karunanithi             bool foundDumpEntry = false;
565b47452b2SAsmitha Karunanithi             std::string dumpEntryPath =
566b47452b2SAsmitha Karunanithi                 "/xyz/openbmc_project/dump/" +
567b47452b2SAsmitha Karunanithi                 std::string(boost::algorithm::to_lower_copy(dumpType)) +
568b47452b2SAsmitha Karunanithi                 "/entry/";
569b47452b2SAsmitha Karunanithi 
5705cb1dd27SAsmitha Karunanithi             for (auto& objectPath : resp)
5715cb1dd27SAsmitha Karunanithi             {
572b47452b2SAsmitha Karunanithi                 if (objectPath.first.str != dumpEntryPath + entryID)
5735cb1dd27SAsmitha Karunanithi                 {
5745cb1dd27SAsmitha Karunanithi                     continue;
5755cb1dd27SAsmitha Karunanithi                 }
5765cb1dd27SAsmitha Karunanithi 
5775cb1dd27SAsmitha Karunanithi                 foundDumpEntry = true;
5781d8782e7SNan Zhou                 uint64_t timestamp = 0;
5795cb1dd27SAsmitha Karunanithi                 uint64_t size = 0;
58035440d18SAsmitha Karunanithi                 std::string dumpStatus;
5815cb1dd27SAsmitha Karunanithi 
5825cb1dd27SAsmitha Karunanithi                 for (auto& interfaceMap : objectPath.second)
5835cb1dd27SAsmitha Karunanithi                 {
58435440d18SAsmitha Karunanithi                     if (interfaceMap.first ==
58535440d18SAsmitha Karunanithi                         "xyz.openbmc_project.Common.Progress")
58635440d18SAsmitha Karunanithi                     {
58735440d18SAsmitha Karunanithi                         for (auto& propertyMap : interfaceMap.second)
58835440d18SAsmitha Karunanithi                         {
58935440d18SAsmitha Karunanithi                             if (propertyMap.first == "Status")
59035440d18SAsmitha Karunanithi                             {
59135440d18SAsmitha Karunanithi                                 auto status = std::get_if<std::string>(
59235440d18SAsmitha Karunanithi                                     &propertyMap.second);
59335440d18SAsmitha Karunanithi                                 if (status == nullptr)
59435440d18SAsmitha Karunanithi                                 {
59535440d18SAsmitha Karunanithi                                     messages::internalError(asyncResp->res);
59635440d18SAsmitha Karunanithi                                     break;
59735440d18SAsmitha Karunanithi                                 }
59835440d18SAsmitha Karunanithi                                 dumpStatus = *status;
59935440d18SAsmitha Karunanithi                             }
60035440d18SAsmitha Karunanithi                         }
60135440d18SAsmitha Karunanithi                     }
60235440d18SAsmitha Karunanithi                     else if (interfaceMap.first ==
60335440d18SAsmitha Karunanithi                              "xyz.openbmc_project.Dump.Entry")
6045cb1dd27SAsmitha Karunanithi                     {
6055cb1dd27SAsmitha Karunanithi                         for (auto& propertyMap : interfaceMap.second)
6065cb1dd27SAsmitha Karunanithi                         {
6075cb1dd27SAsmitha Karunanithi                             if (propertyMap.first == "Size")
6085cb1dd27SAsmitha Karunanithi                             {
6095cb1dd27SAsmitha Karunanithi                                 auto sizePtr =
6105cb1dd27SAsmitha Karunanithi                                     std::get_if<uint64_t>(&propertyMap.second);
6115cb1dd27SAsmitha Karunanithi                                 if (sizePtr == nullptr)
6125cb1dd27SAsmitha Karunanithi                                 {
6135cb1dd27SAsmitha Karunanithi                                     messages::internalError(asyncResp->res);
6145cb1dd27SAsmitha Karunanithi                                     break;
6155cb1dd27SAsmitha Karunanithi                                 }
6165cb1dd27SAsmitha Karunanithi                                 size = *sizePtr;
6175cb1dd27SAsmitha Karunanithi                                 break;
6185cb1dd27SAsmitha Karunanithi                             }
6195cb1dd27SAsmitha Karunanithi                         }
6205cb1dd27SAsmitha Karunanithi                     }
6215cb1dd27SAsmitha Karunanithi                     else if (interfaceMap.first ==
6225cb1dd27SAsmitha Karunanithi                              "xyz.openbmc_project.Time.EpochTime")
6235cb1dd27SAsmitha Karunanithi                     {
6245cb1dd27SAsmitha Karunanithi                         for (auto& propertyMap : interfaceMap.second)
6255cb1dd27SAsmitha Karunanithi                         {
6265cb1dd27SAsmitha Karunanithi                             if (propertyMap.first == "Elapsed")
6275cb1dd27SAsmitha Karunanithi                             {
6285cb1dd27SAsmitha Karunanithi                                 const uint64_t* usecsTimeStamp =
6295cb1dd27SAsmitha Karunanithi                                     std::get_if<uint64_t>(&propertyMap.second);
6305cb1dd27SAsmitha Karunanithi                                 if (usecsTimeStamp == nullptr)
6315cb1dd27SAsmitha Karunanithi                                 {
6325cb1dd27SAsmitha Karunanithi                                     messages::internalError(asyncResp->res);
6335cb1dd27SAsmitha Karunanithi                                     break;
6345cb1dd27SAsmitha Karunanithi                                 }
6351d8782e7SNan Zhou                                 timestamp = *usecsTimeStamp / 1000 / 1000;
6365cb1dd27SAsmitha Karunanithi                                 break;
6375cb1dd27SAsmitha Karunanithi                             }
6385cb1dd27SAsmitha Karunanithi                         }
6395cb1dd27SAsmitha Karunanithi                     }
6405cb1dd27SAsmitha Karunanithi                 }
6415cb1dd27SAsmitha Karunanithi 
6420fda0f12SGeorge Liu                 if (dumpStatus !=
6430fda0f12SGeorge Liu                         "xyz.openbmc_project.Common.Progress.OperationStatus.Completed" &&
64435440d18SAsmitha Karunanithi                     !dumpStatus.empty())
64535440d18SAsmitha Karunanithi                 {
64635440d18SAsmitha Karunanithi                     // Dump status is not Complete
64735440d18SAsmitha Karunanithi                     // return not found until status is changed to Completed
64835440d18SAsmitha Karunanithi                     messages::resourceNotFound(asyncResp->res,
64935440d18SAsmitha Karunanithi                                                dumpType + " dump", entryID);
65035440d18SAsmitha Karunanithi                     return;
65135440d18SAsmitha Karunanithi                 }
65235440d18SAsmitha Karunanithi 
6535cb1dd27SAsmitha Karunanithi                 asyncResp->res.jsonValue["@odata.type"] =
654647b3cdcSGeorge Liu                     "#LogEntry.v1_8_0.LogEntry";
6555cb1dd27SAsmitha Karunanithi                 asyncResp->res.jsonValue["@odata.id"] = dumpPath + entryID;
6565cb1dd27SAsmitha Karunanithi                 asyncResp->res.jsonValue["Id"] = entryID;
6575cb1dd27SAsmitha Karunanithi                 asyncResp->res.jsonValue["EntryType"] = "Event";
6585cb1dd27SAsmitha Karunanithi                 asyncResp->res.jsonValue["Created"] =
6591d8782e7SNan Zhou                     crow::utility::getDateTimeUint(timestamp);
6605cb1dd27SAsmitha Karunanithi                 asyncResp->res.jsonValue["Name"] = dumpType + " Dump Entry";
6615cb1dd27SAsmitha Karunanithi 
662d337bb72SAsmitha Karunanithi                 asyncResp->res.jsonValue["AdditionalDataSizeBytes"] = size;
6635cb1dd27SAsmitha Karunanithi 
6645cb1dd27SAsmitha Karunanithi                 if (dumpType == "BMC")
6655cb1dd27SAsmitha Karunanithi                 {
666d337bb72SAsmitha Karunanithi                     asyncResp->res.jsonValue["DiagnosticDataType"] = "Manager";
667d337bb72SAsmitha Karunanithi                     asyncResp->res.jsonValue["AdditionalDataURI"] =
668de8d94a3SAbhishek Patel                         "/redfish/v1/Managers/bmc/LogServices/Dump/Entries/" +
669de8d94a3SAbhishek Patel                         entryID + "/attachment";
6705cb1dd27SAsmitha Karunanithi                 }
6715cb1dd27SAsmitha Karunanithi                 else if (dumpType == "System")
6725cb1dd27SAsmitha Karunanithi                 {
673d337bb72SAsmitha Karunanithi                     asyncResp->res.jsonValue["DiagnosticDataType"] = "OEM";
674d337bb72SAsmitha Karunanithi                     asyncResp->res.jsonValue["OEMDiagnosticDataType"] =
6755cb1dd27SAsmitha Karunanithi                         "System";
676d337bb72SAsmitha Karunanithi                     asyncResp->res.jsonValue["AdditionalDataURI"] =
677de8d94a3SAbhishek Patel                         "/redfish/v1/Systems/system/LogServices/Dump/Entries/" +
678de8d94a3SAbhishek Patel                         entryID + "/attachment";
6795cb1dd27SAsmitha Karunanithi                 }
6805cb1dd27SAsmitha Karunanithi             }
681b47452b2SAsmitha Karunanithi             if (foundDumpEntry == false)
682b47452b2SAsmitha Karunanithi             {
683b47452b2SAsmitha Karunanithi                 BMCWEB_LOG_ERROR << "Can't find Dump Entry";
684b47452b2SAsmitha Karunanithi                 messages::internalError(asyncResp->res);
685b47452b2SAsmitha Karunanithi                 return;
686b47452b2SAsmitha Karunanithi             }
6875cb1dd27SAsmitha Karunanithi         },
6885cb1dd27SAsmitha Karunanithi         "xyz.openbmc_project.Dump.Manager", "/xyz/openbmc_project/dump",
6895cb1dd27SAsmitha Karunanithi         "org.freedesktop.DBus.ObjectManager", "GetManagedObjects");
6905cb1dd27SAsmitha Karunanithi }
6915cb1dd27SAsmitha Karunanithi 
6928d1b46d7Szhanghch05 inline void deleteDumpEntry(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
6939878256fSStanley Chu                             const std::string& entryID,
694b47452b2SAsmitha Karunanithi                             const std::string& dumpType)
6955cb1dd27SAsmitha Karunanithi {
6963de8d8baSGeorge Liu     auto respHandler = [asyncResp,
6973de8d8baSGeorge Liu                         entryID](const boost::system::error_code ec) {
6985cb1dd27SAsmitha Karunanithi         BMCWEB_LOG_DEBUG << "Dump Entry doDelete callback: Done";
6995cb1dd27SAsmitha Karunanithi         if (ec)
7005cb1dd27SAsmitha Karunanithi         {
7013de8d8baSGeorge Liu             if (ec.value() == EBADR)
7023de8d8baSGeorge Liu             {
7033de8d8baSGeorge Liu                 messages::resourceNotFound(asyncResp->res, "LogEntry", entryID);
7043de8d8baSGeorge Liu                 return;
7053de8d8baSGeorge Liu             }
7065cb1dd27SAsmitha Karunanithi             BMCWEB_LOG_ERROR << "Dump (DBus) doDelete respHandler got error "
7075cb1dd27SAsmitha Karunanithi                              << ec;
7085cb1dd27SAsmitha Karunanithi             messages::internalError(asyncResp->res);
7095cb1dd27SAsmitha Karunanithi             return;
7105cb1dd27SAsmitha Karunanithi         }
7115cb1dd27SAsmitha Karunanithi     };
7125cb1dd27SAsmitha Karunanithi     crow::connections::systemBus->async_method_call(
7135cb1dd27SAsmitha Karunanithi         respHandler, "xyz.openbmc_project.Dump.Manager",
714b47452b2SAsmitha Karunanithi         "/xyz/openbmc_project/dump/" +
715b47452b2SAsmitha Karunanithi             std::string(boost::algorithm::to_lower_copy(dumpType)) + "/entry/" +
716b47452b2SAsmitha Karunanithi             entryID,
7175cb1dd27SAsmitha Karunanithi         "xyz.openbmc_project.Object.Delete", "Delete");
7185cb1dd27SAsmitha Karunanithi }
7195cb1dd27SAsmitha Karunanithi 
7208d1b46d7Szhanghch05 inline void
72198be3e39SEd Tanous     createDumpTaskCallback(task::Payload&& payload,
7228d1b46d7Szhanghch05                            const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
7238d1b46d7Szhanghch05                            const uint32_t& dumpId, const std::string& dumpPath,
724a43be80fSAsmitha Karunanithi                            const std::string& dumpType)
725a43be80fSAsmitha Karunanithi {
726a43be80fSAsmitha Karunanithi     std::shared_ptr<task::TaskData> task = task::TaskData::createTask(
7276145ed6fSAsmitha Karunanithi         [dumpId, dumpPath, dumpType](
728a43be80fSAsmitha Karunanithi             boost::system::error_code err, sdbusplus::message::message& m,
729a43be80fSAsmitha Karunanithi             const std::shared_ptr<task::TaskData>& taskData) {
730cb13a392SEd Tanous             if (err)
731cb13a392SEd Tanous             {
7326145ed6fSAsmitha Karunanithi                 BMCWEB_LOG_ERROR << "Error in creating a dump";
7336145ed6fSAsmitha Karunanithi                 taskData->state = "Cancelled";
7346145ed6fSAsmitha Karunanithi                 return task::completed;
735cb13a392SEd Tanous             }
736a43be80fSAsmitha Karunanithi             std::vector<std::pair<
737168e20c1SEd Tanous                 std::string, std::vector<std::pair<
738168e20c1SEd Tanous                                  std::string, dbus::utility::DbusVariantType>>>>
739a43be80fSAsmitha Karunanithi                 interfacesList;
740a43be80fSAsmitha Karunanithi 
741a43be80fSAsmitha Karunanithi             sdbusplus::message::object_path objPath;
742a43be80fSAsmitha Karunanithi 
743a43be80fSAsmitha Karunanithi             m.read(objPath, interfacesList);
744a43be80fSAsmitha Karunanithi 
745b47452b2SAsmitha Karunanithi             if (objPath.str ==
746b47452b2SAsmitha Karunanithi                 "/xyz/openbmc_project/dump/" +
747b47452b2SAsmitha Karunanithi                     std::string(boost::algorithm::to_lower_copy(dumpType)) +
748b47452b2SAsmitha Karunanithi                     "/entry/" + std::to_string(dumpId))
749a43be80fSAsmitha Karunanithi             {
750a43be80fSAsmitha Karunanithi                 nlohmann::json retMessage = messages::success();
751a43be80fSAsmitha Karunanithi                 taskData->messages.emplace_back(retMessage);
752a43be80fSAsmitha Karunanithi 
753a43be80fSAsmitha Karunanithi                 std::string headerLoc =
754a43be80fSAsmitha Karunanithi                     "Location: " + dumpPath + std::to_string(dumpId);
755a43be80fSAsmitha Karunanithi                 taskData->payload->httpHeaders.emplace_back(
756a43be80fSAsmitha Karunanithi                     std::move(headerLoc));
757a43be80fSAsmitha Karunanithi 
758a43be80fSAsmitha Karunanithi                 taskData->state = "Completed";
759b47452b2SAsmitha Karunanithi                 return task::completed;
7606145ed6fSAsmitha Karunanithi             }
761a43be80fSAsmitha Karunanithi             return task::completed;
762a43be80fSAsmitha Karunanithi         },
763a43be80fSAsmitha Karunanithi         "type='signal',interface='org.freedesktop.DBus."
764a43be80fSAsmitha Karunanithi         "ObjectManager',"
765a43be80fSAsmitha Karunanithi         "member='InterfacesAdded', "
766a43be80fSAsmitha Karunanithi         "path='/xyz/openbmc_project/dump'");
767a43be80fSAsmitha Karunanithi 
768a43be80fSAsmitha Karunanithi     task->startTimer(std::chrono::minutes(3));
769a43be80fSAsmitha Karunanithi     task->populateResp(asyncResp->res);
77098be3e39SEd Tanous     task->payload.emplace(std::move(payload));
771a43be80fSAsmitha Karunanithi }
772a43be80fSAsmitha Karunanithi 
7738d1b46d7Szhanghch05 inline void createDump(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
7748d1b46d7Szhanghch05                        const crow::Request& req, const std::string& dumpType)
775a43be80fSAsmitha Karunanithi {
776a43be80fSAsmitha Karunanithi 
777a43be80fSAsmitha Karunanithi     std::string dumpPath;
778a43be80fSAsmitha Karunanithi     if (dumpType == "BMC")
779a43be80fSAsmitha Karunanithi     {
780a43be80fSAsmitha Karunanithi         dumpPath = "/redfish/v1/Managers/bmc/LogServices/Dump/Entries/";
781a43be80fSAsmitha Karunanithi     }
782a43be80fSAsmitha Karunanithi     else if (dumpType == "System")
783a43be80fSAsmitha Karunanithi     {
784a43be80fSAsmitha Karunanithi         dumpPath = "/redfish/v1/Systems/system/LogServices/Dump/Entries/";
785a43be80fSAsmitha Karunanithi     }
786a43be80fSAsmitha Karunanithi     else
787a43be80fSAsmitha Karunanithi     {
788a43be80fSAsmitha Karunanithi         BMCWEB_LOG_ERROR << "Invalid dump type: " << dumpType;
789a43be80fSAsmitha Karunanithi         messages::internalError(asyncResp->res);
790a43be80fSAsmitha Karunanithi         return;
791a43be80fSAsmitha Karunanithi     }
792a43be80fSAsmitha Karunanithi 
793a43be80fSAsmitha Karunanithi     std::optional<std::string> diagnosticDataType;
794a43be80fSAsmitha Karunanithi     std::optional<std::string> oemDiagnosticDataType;
795a43be80fSAsmitha Karunanithi 
796a43be80fSAsmitha Karunanithi     if (!redfish::json_util::readJson(
797a43be80fSAsmitha Karunanithi             req, asyncResp->res, "DiagnosticDataType", diagnosticDataType,
798a43be80fSAsmitha Karunanithi             "OEMDiagnosticDataType", oemDiagnosticDataType))
799a43be80fSAsmitha Karunanithi     {
800a43be80fSAsmitha Karunanithi         return;
801a43be80fSAsmitha Karunanithi     }
802a43be80fSAsmitha Karunanithi 
803a43be80fSAsmitha Karunanithi     if (dumpType == "System")
804a43be80fSAsmitha Karunanithi     {
805a43be80fSAsmitha Karunanithi         if (!oemDiagnosticDataType || !diagnosticDataType)
806a43be80fSAsmitha Karunanithi         {
807a43be80fSAsmitha Karunanithi             BMCWEB_LOG_ERROR << "CreateDump action parameter "
808a43be80fSAsmitha Karunanithi                                 "'DiagnosticDataType'/"
809a43be80fSAsmitha Karunanithi                                 "'OEMDiagnosticDataType' value not found!";
810a43be80fSAsmitha Karunanithi             messages::actionParameterMissing(
811a43be80fSAsmitha Karunanithi                 asyncResp->res, "CollectDiagnosticData",
812a43be80fSAsmitha Karunanithi                 "DiagnosticDataType & OEMDiagnosticDataType");
813a43be80fSAsmitha Karunanithi             return;
814a43be80fSAsmitha Karunanithi         }
8153174e4dfSEd Tanous         if ((*oemDiagnosticDataType != "System") ||
816a43be80fSAsmitha Karunanithi             (*diagnosticDataType != "OEM"))
817a43be80fSAsmitha Karunanithi         {
818a43be80fSAsmitha Karunanithi             BMCWEB_LOG_ERROR << "Wrong parameter values passed";
819a43be80fSAsmitha Karunanithi             messages::invalidObject(asyncResp->res,
820a43be80fSAsmitha Karunanithi                                     "System Dump creation parameters");
821a43be80fSAsmitha Karunanithi             return;
822a43be80fSAsmitha Karunanithi         }
823a43be80fSAsmitha Karunanithi     }
824a43be80fSAsmitha Karunanithi     else if (dumpType == "BMC")
825a43be80fSAsmitha Karunanithi     {
826a43be80fSAsmitha Karunanithi         if (!diagnosticDataType)
827a43be80fSAsmitha Karunanithi         {
8280fda0f12SGeorge Liu             BMCWEB_LOG_ERROR
8290fda0f12SGeorge Liu                 << "CreateDump action parameter 'DiagnosticDataType' not found!";
830a43be80fSAsmitha Karunanithi             messages::actionParameterMissing(
831a43be80fSAsmitha Karunanithi                 asyncResp->res, "CollectDiagnosticData", "DiagnosticDataType");
832a43be80fSAsmitha Karunanithi             return;
833a43be80fSAsmitha Karunanithi         }
8343174e4dfSEd Tanous         if (*diagnosticDataType != "Manager")
835a43be80fSAsmitha Karunanithi         {
836a43be80fSAsmitha Karunanithi             BMCWEB_LOG_ERROR
837a43be80fSAsmitha Karunanithi                 << "Wrong parameter value passed for 'DiagnosticDataType'";
838a43be80fSAsmitha Karunanithi             messages::invalidObject(asyncResp->res,
839a43be80fSAsmitha Karunanithi                                     "BMC Dump creation parameters");
840a43be80fSAsmitha Karunanithi             return;
841a43be80fSAsmitha Karunanithi         }
842a43be80fSAsmitha Karunanithi     }
843a43be80fSAsmitha Karunanithi 
844a43be80fSAsmitha Karunanithi     crow::connections::systemBus->async_method_call(
84598be3e39SEd Tanous         [asyncResp, payload(task::Payload(req)), dumpPath,
84698be3e39SEd Tanous          dumpType](const boost::system::error_code ec,
84798be3e39SEd Tanous                    const uint32_t& dumpId) mutable {
848a43be80fSAsmitha Karunanithi             if (ec)
849a43be80fSAsmitha Karunanithi             {
850a43be80fSAsmitha Karunanithi                 BMCWEB_LOG_ERROR << "CreateDump resp_handler got error " << ec;
851a43be80fSAsmitha Karunanithi                 messages::internalError(asyncResp->res);
852a43be80fSAsmitha Karunanithi                 return;
853a43be80fSAsmitha Karunanithi             }
854a43be80fSAsmitha Karunanithi             BMCWEB_LOG_DEBUG << "Dump Created. Id: " << dumpId;
855a43be80fSAsmitha Karunanithi 
85698be3e39SEd Tanous             createDumpTaskCallback(std::move(payload), asyncResp, dumpId,
85798be3e39SEd Tanous                                    dumpPath, dumpType);
858a43be80fSAsmitha Karunanithi         },
859b47452b2SAsmitha Karunanithi         "xyz.openbmc_project.Dump.Manager",
860b47452b2SAsmitha Karunanithi         "/xyz/openbmc_project/dump/" +
861b47452b2SAsmitha Karunanithi             std::string(boost::algorithm::to_lower_copy(dumpType)),
862a43be80fSAsmitha Karunanithi         "xyz.openbmc_project.Dump.Create", "CreateDump");
863a43be80fSAsmitha Karunanithi }
864a43be80fSAsmitha Karunanithi 
8658d1b46d7Szhanghch05 inline void clearDump(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
8668d1b46d7Szhanghch05                       const std::string& dumpType)
86780319af1SAsmitha Karunanithi {
868b47452b2SAsmitha Karunanithi     std::string dumpTypeLowerCopy =
869b47452b2SAsmitha Karunanithi         std::string(boost::algorithm::to_lower_copy(dumpType));
8708d1b46d7Szhanghch05 
87180319af1SAsmitha Karunanithi     crow::connections::systemBus->async_method_call(
872b47452b2SAsmitha Karunanithi         [asyncResp, dumpType](const boost::system::error_code ec,
87380319af1SAsmitha Karunanithi                               const std::vector<std::string>& subTreePaths) {
87480319af1SAsmitha Karunanithi             if (ec)
87580319af1SAsmitha Karunanithi             {
87680319af1SAsmitha Karunanithi                 BMCWEB_LOG_ERROR << "resp_handler got error " << ec;
87780319af1SAsmitha Karunanithi                 messages::internalError(asyncResp->res);
87880319af1SAsmitha Karunanithi                 return;
87980319af1SAsmitha Karunanithi             }
88080319af1SAsmitha Karunanithi 
88180319af1SAsmitha Karunanithi             for (const std::string& path : subTreePaths)
88280319af1SAsmitha Karunanithi             {
8832dfd18efSEd Tanous                 sdbusplus::message::object_path objPath(path);
8842dfd18efSEd Tanous                 std::string logID = objPath.filename();
8852dfd18efSEd Tanous                 if (logID.empty())
88680319af1SAsmitha Karunanithi                 {
8872dfd18efSEd Tanous                     continue;
88880319af1SAsmitha Karunanithi                 }
8892dfd18efSEd Tanous                 deleteDumpEntry(asyncResp, logID, dumpType);
89080319af1SAsmitha Karunanithi             }
89180319af1SAsmitha Karunanithi         },
89280319af1SAsmitha Karunanithi         "xyz.openbmc_project.ObjectMapper",
89380319af1SAsmitha Karunanithi         "/xyz/openbmc_project/object_mapper",
89480319af1SAsmitha Karunanithi         "xyz.openbmc_project.ObjectMapper", "GetSubTreePaths",
895b47452b2SAsmitha Karunanithi         "/xyz/openbmc_project/dump/" + dumpTypeLowerCopy, 0,
896b47452b2SAsmitha Karunanithi         std::array<std::string, 1>{"xyz.openbmc_project.Dump.Entry." +
897b47452b2SAsmitha Karunanithi                                    dumpType});
89880319af1SAsmitha Karunanithi }
89980319af1SAsmitha Karunanithi 
9007e860f15SJohn Edward Broadbent inline static void parseCrashdumpParameters(
901168e20c1SEd Tanous     const std::vector<std::pair<std::string, dbus::utility::DbusVariantType>>&
902168e20c1SEd Tanous         params,
903043a0536SJohnathan Mantey     std::string& filename, std::string& timestamp, std::string& logfile)
904043a0536SJohnathan Mantey {
905043a0536SJohnathan Mantey     for (auto property : params)
906043a0536SJohnathan Mantey     {
907043a0536SJohnathan Mantey         if (property.first == "Timestamp")
908043a0536SJohnathan Mantey         {
909043a0536SJohnathan Mantey             const std::string* value =
9108d78b7a9SPatrick Williams                 std::get_if<std::string>(&property.second);
911043a0536SJohnathan Mantey             if (value != nullptr)
912043a0536SJohnathan Mantey             {
913043a0536SJohnathan Mantey                 timestamp = *value;
914043a0536SJohnathan Mantey             }
915043a0536SJohnathan Mantey         }
916043a0536SJohnathan Mantey         else if (property.first == "Filename")
917043a0536SJohnathan Mantey         {
918043a0536SJohnathan Mantey             const std::string* value =
9198d78b7a9SPatrick Williams                 std::get_if<std::string>(&property.second);
920043a0536SJohnathan Mantey             if (value != nullptr)
921043a0536SJohnathan Mantey             {
922043a0536SJohnathan Mantey                 filename = *value;
923043a0536SJohnathan Mantey             }
924043a0536SJohnathan Mantey         }
925043a0536SJohnathan Mantey         else if (property.first == "Log")
926043a0536SJohnathan Mantey         {
927043a0536SJohnathan Mantey             const std::string* value =
9288d78b7a9SPatrick Williams                 std::get_if<std::string>(&property.second);
929043a0536SJohnathan Mantey             if (value != nullptr)
930043a0536SJohnathan Mantey             {
931043a0536SJohnathan Mantey                 logfile = *value;
932043a0536SJohnathan Mantey             }
933043a0536SJohnathan Mantey         }
934043a0536SJohnathan Mantey     }
935043a0536SJohnathan Mantey }
936043a0536SJohnathan Mantey 
937a3316fc6SZhikuiRen constexpr char const* postCodeIface = "xyz.openbmc_project.State.Boot.PostCode";
9387e860f15SJohn Edward Broadbent inline void requestRoutesSystemLogServiceCollection(App& app)
9391da66f75SEd Tanous {
940c4bf6374SJason M. Bills     /**
941c4bf6374SJason M. Bills      * Functions triggers appropriate requests on DBus
942c4bf6374SJason M. Bills      */
9437e860f15SJohn Edward Broadbent     BMCWEB_ROUTE(app, "/redfish/v1/Systems/system/LogServices/")
944ed398213SEd Tanous         .privileges(redfish::privileges::getLogServiceCollection)
9457e860f15SJohn Edward Broadbent         .methods(boost::beast::http::verb::get)(
9467e860f15SJohn Edward Broadbent             [](const crow::Request&,
9477e860f15SJohn Edward Broadbent                const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
9487e860f15SJohn Edward Broadbent 
949c4bf6374SJason M. Bills             {
9507e860f15SJohn Edward Broadbent                 // Collections don't include the static data added by SubRoute
9517e860f15SJohn Edward Broadbent                 // because it has a duplicate entry for members
952c4bf6374SJason M. Bills                 asyncResp->res.jsonValue["@odata.type"] =
953c4bf6374SJason M. Bills                     "#LogServiceCollection.LogServiceCollection";
954c4bf6374SJason M. Bills                 asyncResp->res.jsonValue["@odata.id"] =
955029573d4SEd Tanous                     "/redfish/v1/Systems/system/LogServices";
9567e860f15SJohn Edward Broadbent                 asyncResp->res.jsonValue["Name"] =
9577e860f15SJohn Edward Broadbent                     "System Log Services Collection";
958c4bf6374SJason M. Bills                 asyncResp->res.jsonValue["Description"] =
959c4bf6374SJason M. Bills                     "Collection of LogServices for this Computer System";
9607e860f15SJohn Edward Broadbent                 nlohmann::json& logServiceArray =
9617e860f15SJohn Edward Broadbent                     asyncResp->res.jsonValue["Members"];
962c4bf6374SJason M. Bills                 logServiceArray = nlohmann::json::array();
963029573d4SEd Tanous                 logServiceArray.push_back(
9647e860f15SJohn Edward Broadbent                     {{"@odata.id",
9657e860f15SJohn Edward Broadbent                       "/redfish/v1/Systems/system/LogServices/EventLog"}});
9665cb1dd27SAsmitha Karunanithi #ifdef BMCWEB_ENABLE_REDFISH_DUMP_LOG
967c9bb6861Sraviteja-b                 logServiceArray.push_back(
9687e860f15SJohn Edward Broadbent                     {{"@odata.id",
9697e860f15SJohn Edward Broadbent                       "/redfish/v1/Systems/system/LogServices/Dump"}});
970c9bb6861Sraviteja-b #endif
971c9bb6861Sraviteja-b 
972d53dd41fSJason M. Bills #ifdef BMCWEB_ENABLE_REDFISH_CPU_LOG
973d53dd41fSJason M. Bills                 logServiceArray.push_back(
974cb92c03bSAndrew Geissler                     {{"@odata.id",
975424c4176SJason M. Bills                       "/redfish/v1/Systems/system/LogServices/Crashdump"}});
976d53dd41fSJason M. Bills #endif
977b7028ebfSSpencer Ku 
978b7028ebfSSpencer Ku #ifdef BMCWEB_ENABLE_REDFISH_HOST_LOGGER
979b7028ebfSSpencer Ku                 logServiceArray.push_back(
980b7028ebfSSpencer Ku                     {{"@odata.id",
981b7028ebfSSpencer Ku                       "/redfish/v1/Systems/system/LogServices/HostLogger"}});
982b7028ebfSSpencer Ku #endif
983c4bf6374SJason M. Bills                 asyncResp->res.jsonValue["Members@odata.count"] =
984c4bf6374SJason M. Bills                     logServiceArray.size();
985a3316fc6SZhikuiRen 
986a3316fc6SZhikuiRen                 crow::connections::systemBus->async_method_call(
987a3316fc6SZhikuiRen                     [asyncResp](const boost::system::error_code ec,
988a3316fc6SZhikuiRen                                 const std::vector<std::string>& subtreePath) {
989a3316fc6SZhikuiRen                         if (ec)
990a3316fc6SZhikuiRen                         {
991a3316fc6SZhikuiRen                             BMCWEB_LOG_ERROR << ec;
992a3316fc6SZhikuiRen                             return;
993a3316fc6SZhikuiRen                         }
994a3316fc6SZhikuiRen 
995a3316fc6SZhikuiRen                         for (auto& pathStr : subtreePath)
996a3316fc6SZhikuiRen                         {
997a3316fc6SZhikuiRen                             if (pathStr.find("PostCode") != std::string::npos)
998a3316fc6SZhikuiRen                             {
99923a21a1cSEd Tanous                                 nlohmann::json& logServiceArrayLocal =
1000a3316fc6SZhikuiRen                                     asyncResp->res.jsonValue["Members"];
100123a21a1cSEd Tanous                                 logServiceArrayLocal.push_back(
10020fda0f12SGeorge Liu                                     {{"@odata.id",
10030fda0f12SGeorge Liu                                       "/redfish/v1/Systems/system/LogServices/PostCodes"}});
10047e860f15SJohn Edward Broadbent                                 asyncResp->res
10057e860f15SJohn Edward Broadbent                                     .jsonValue["Members@odata.count"] =
100623a21a1cSEd Tanous                                     logServiceArrayLocal.size();
1007a3316fc6SZhikuiRen                                 return;
1008a3316fc6SZhikuiRen                             }
1009a3316fc6SZhikuiRen                         }
1010a3316fc6SZhikuiRen                     },
1011a3316fc6SZhikuiRen                     "xyz.openbmc_project.ObjectMapper",
1012a3316fc6SZhikuiRen                     "/xyz/openbmc_project/object_mapper",
10137e860f15SJohn Edward Broadbent                     "xyz.openbmc_project.ObjectMapper", "GetSubTreePaths", "/",
10147e860f15SJohn Edward Broadbent                     0, std::array<const char*, 1>{postCodeIface});
10157e860f15SJohn Edward Broadbent             });
1016c4bf6374SJason M. Bills }
1017c4bf6374SJason M. Bills 
10187e860f15SJohn Edward Broadbent inline void requestRoutesEventLogService(App& app)
1019c4bf6374SJason M. Bills {
10207e860f15SJohn Edward Broadbent     BMCWEB_ROUTE(app, "/redfish/v1/Systems/system/LogServices/EventLog/")
1021ed398213SEd Tanous         .privileges(redfish::privileges::getLogService)
10227e860f15SJohn Edward Broadbent         .methods(
10237e860f15SJohn Edward Broadbent             boost::beast::http::verb::
10247e860f15SJohn Edward Broadbent                 get)([](const crow::Request&,
10257e860f15SJohn Edward Broadbent                         const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) {
1026c4bf6374SJason M. Bills             asyncResp->res.jsonValue["@odata.id"] =
1027029573d4SEd Tanous                 "/redfish/v1/Systems/system/LogServices/EventLog";
1028c4bf6374SJason M. Bills             asyncResp->res.jsonValue["@odata.type"] =
1029c4bf6374SJason M. Bills                 "#LogService.v1_1_0.LogService";
1030c4bf6374SJason M. Bills             asyncResp->res.jsonValue["Name"] = "Event Log Service";
10317e860f15SJohn Edward Broadbent             asyncResp->res.jsonValue["Description"] =
10327e860f15SJohn Edward Broadbent                 "System Event Log Service";
1033c4bf6374SJason M. Bills             asyncResp->res.jsonValue["Id"] = "EventLog";
1034c4bf6374SJason M. Bills             asyncResp->res.jsonValue["OverWritePolicy"] = "WrapsWhenFull";
10357c8c4058STejas Patil 
10367c8c4058STejas Patil             std::pair<std::string, std::string> redfishDateTimeOffset =
10377c8c4058STejas Patil                 crow::utility::getDateTimeOffsetNow();
10387c8c4058STejas Patil 
10397c8c4058STejas Patil             asyncResp->res.jsonValue["DateTime"] = redfishDateTimeOffset.first;
10407c8c4058STejas Patil             asyncResp->res.jsonValue["DateTimeLocalOffset"] =
10417c8c4058STejas Patil                 redfishDateTimeOffset.second;
10427c8c4058STejas Patil 
1043c4bf6374SJason M. Bills             asyncResp->res.jsonValue["Entries"] = {
1044c4bf6374SJason M. Bills                 {"@odata.id",
1045029573d4SEd Tanous                  "/redfish/v1/Systems/system/LogServices/EventLog/Entries"}};
1046e7d6c8b2SGunnar Mills             asyncResp->res.jsonValue["Actions"]["#LogService.ClearLog"] = {
1047e7d6c8b2SGunnar Mills 
10480fda0f12SGeorge Liu                 {"target",
10490fda0f12SGeorge Liu                  "/redfish/v1/Systems/system/LogServices/EventLog/Actions/LogService.ClearLog"}};
10507e860f15SJohn Edward Broadbent         });
1051489640c6SJason M. Bills }
1052489640c6SJason M. Bills 
10537e860f15SJohn Edward Broadbent inline void requestRoutesJournalEventLogClear(App& app)
1054489640c6SJason M. Bills {
10557e860f15SJohn Edward Broadbent     BMCWEB_ROUTE(app, "/redfish/v1/Systems/system/LogServices/EventLog/Actions/"
1056489640c6SJason M. Bills                       "LogService.ClearLog/")
1057432a890cSEd Tanous         .privileges({{"ConfigureComponents"}})
10587e860f15SJohn Edward Broadbent         .methods(boost::beast::http::verb::post)(
10597e860f15SJohn Edward Broadbent             [](const crow::Request&,
10607e860f15SJohn Edward Broadbent                const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) {
1061489640c6SJason M. Bills                 // Clear the EventLog by deleting the log files
1062489640c6SJason M. Bills                 std::vector<std::filesystem::path> redfishLogFiles;
1063489640c6SJason M. Bills                 if (getRedfishLogFiles(redfishLogFiles))
1064489640c6SJason M. Bills                 {
1065489640c6SJason M. Bills                     for (const std::filesystem::path& file : redfishLogFiles)
1066489640c6SJason M. Bills                     {
1067489640c6SJason M. Bills                         std::error_code ec;
1068489640c6SJason M. Bills                         std::filesystem::remove(file, ec);
1069489640c6SJason M. Bills                     }
1070489640c6SJason M. Bills                 }
1071489640c6SJason M. Bills 
1072489640c6SJason M. Bills                 // Reload rsyslog so it knows to start new log files
1073489640c6SJason M. Bills                 crow::connections::systemBus->async_method_call(
1074489640c6SJason M. Bills                     [asyncResp](const boost::system::error_code ec) {
1075489640c6SJason M. Bills                         if (ec)
1076489640c6SJason M. Bills                         {
10777e860f15SJohn Edward Broadbent                             BMCWEB_LOG_ERROR << "Failed to reload rsyslog: "
10787e860f15SJohn Edward Broadbent                                              << ec;
1079489640c6SJason M. Bills                             messages::internalError(asyncResp->res);
1080489640c6SJason M. Bills                             return;
1081489640c6SJason M. Bills                         }
1082489640c6SJason M. Bills 
1083489640c6SJason M. Bills                         messages::success(asyncResp->res);
1084489640c6SJason M. Bills                     },
1085489640c6SJason M. Bills                     "org.freedesktop.systemd1", "/org/freedesktop/systemd1",
10867e860f15SJohn Edward Broadbent                     "org.freedesktop.systemd1.Manager", "ReloadUnit",
10877e860f15SJohn Edward Broadbent                     "rsyslog.service", "replace");
10887e860f15SJohn Edward Broadbent             });
1089c4bf6374SJason M. Bills }
1090c4bf6374SJason M. Bills 
109195820184SJason M. Bills static int fillEventLogEntryJson(const std::string& logEntryID,
1092b5a76932SEd Tanous                                  const std::string& logEntry,
109395820184SJason M. Bills                                  nlohmann::json& logEntryJson)
1094c4bf6374SJason M. Bills {
109595820184SJason M. Bills     // The redfish log format is "<Timestamp> <MessageId>,<MessageArgs>"
1096cd225da8SJason M. Bills     // First get the Timestamp
1097f23b7296SEd Tanous     size_t space = logEntry.find_first_of(' ');
1098cd225da8SJason M. Bills     if (space == std::string::npos)
109995820184SJason M. Bills     {
110095820184SJason M. Bills         return 1;
110195820184SJason M. Bills     }
1102cd225da8SJason M. Bills     std::string timestamp = logEntry.substr(0, space);
1103cd225da8SJason M. Bills     // Then get the log contents
1104f23b7296SEd Tanous     size_t entryStart = logEntry.find_first_not_of(' ', space);
1105cd225da8SJason M. Bills     if (entryStart == std::string::npos)
1106cd225da8SJason M. Bills     {
1107cd225da8SJason M. Bills         return 1;
1108cd225da8SJason M. Bills     }
1109cd225da8SJason M. Bills     std::string_view entry(logEntry);
1110cd225da8SJason M. Bills     entry.remove_prefix(entryStart);
1111cd225da8SJason M. Bills     // Use split to separate the entry into its fields
1112cd225da8SJason M. Bills     std::vector<std::string> logEntryFields;
1113cd225da8SJason M. Bills     boost::split(logEntryFields, entry, boost::is_any_of(","),
1114cd225da8SJason M. Bills                  boost::token_compress_on);
1115cd225da8SJason M. Bills     // We need at least a MessageId to be valid
1116cd225da8SJason M. Bills     if (logEntryFields.size() < 1)
1117cd225da8SJason M. Bills     {
1118cd225da8SJason M. Bills         return 1;
1119cd225da8SJason M. Bills     }
1120cd225da8SJason M. Bills     std::string& messageID = logEntryFields[0];
112195820184SJason M. Bills 
11224851d45dSJason M. Bills     // Get the Message from the MessageRegistry
11234851d45dSJason M. Bills     const message_registries::Message* message =
11244851d45dSJason M. Bills         message_registries::getMessage(messageID);
1125c4bf6374SJason M. Bills 
11264851d45dSJason M. Bills     std::string msg;
11274851d45dSJason M. Bills     std::string severity;
11284851d45dSJason M. Bills     if (message != nullptr)
1129c4bf6374SJason M. Bills     {
11304851d45dSJason M. Bills         msg = message->message;
11314851d45dSJason M. Bills         severity = message->severity;
1132c4bf6374SJason M. Bills     }
1133c4bf6374SJason M. Bills 
113415a86ff6SJason M. Bills     // Get the MessageArgs from the log if there are any
113526702d01SEd Tanous     std::span<std::string> messageArgs;
113615a86ff6SJason M. Bills     if (logEntryFields.size() > 1)
113715a86ff6SJason M. Bills     {
113815a86ff6SJason M. Bills         std::string& messageArgsStart = logEntryFields[1];
113915a86ff6SJason M. Bills         // If the first string is empty, assume there are no MessageArgs
114015a86ff6SJason M. Bills         std::size_t messageArgsSize = 0;
114115a86ff6SJason M. Bills         if (!messageArgsStart.empty())
114215a86ff6SJason M. Bills         {
114315a86ff6SJason M. Bills             messageArgsSize = logEntryFields.size() - 1;
114415a86ff6SJason M. Bills         }
114515a86ff6SJason M. Bills 
114623a21a1cSEd Tanous         messageArgs = {&messageArgsStart, messageArgsSize};
1147c4bf6374SJason M. Bills 
11484851d45dSJason M. Bills         // Fill the MessageArgs into the Message
114995820184SJason M. Bills         int i = 0;
115095820184SJason M. Bills         for (const std::string& messageArg : messageArgs)
11514851d45dSJason M. Bills         {
115295820184SJason M. Bills             std::string argStr = "%" + std::to_string(++i);
11534851d45dSJason M. Bills             size_t argPos = msg.find(argStr);
11544851d45dSJason M. Bills             if (argPos != std::string::npos)
11554851d45dSJason M. Bills             {
115695820184SJason M. Bills                 msg.replace(argPos, argStr.length(), messageArg);
11574851d45dSJason M. Bills             }
11584851d45dSJason M. Bills         }
115915a86ff6SJason M. Bills     }
11604851d45dSJason M. Bills 
116195820184SJason M. Bills     // Get the Created time from the timestamp. The log timestamp is in RFC3339
116295820184SJason M. Bills     // format which matches the Redfish format except for the fractional seconds
116395820184SJason M. Bills     // between the '.' and the '+', so just remove them.
1164f23b7296SEd Tanous     std::size_t dot = timestamp.find_first_of('.');
1165f23b7296SEd Tanous     std::size_t plus = timestamp.find_first_of('+');
116695820184SJason M. Bills     if (dot != std::string::npos && plus != std::string::npos)
1167c4bf6374SJason M. Bills     {
116895820184SJason M. Bills         timestamp.erase(dot, plus - dot);
1169c4bf6374SJason M. Bills     }
1170c4bf6374SJason M. Bills 
1171c4bf6374SJason M. Bills     // Fill in the log entry with the gathered data
117295820184SJason M. Bills     logEntryJson = {
1173647b3cdcSGeorge Liu         {"@odata.type", "#LogEntry.v1_8_0.LogEntry"},
1174029573d4SEd Tanous         {"@odata.id",
1175897967deSJason M. Bills          "/redfish/v1/Systems/system/LogServices/EventLog/Entries/" +
117695820184SJason M. Bills              logEntryID},
1177c4bf6374SJason M. Bills         {"Name", "System Event Log Entry"},
117895820184SJason M. Bills         {"Id", logEntryID},
117995820184SJason M. Bills         {"Message", std::move(msg)},
118095820184SJason M. Bills         {"MessageId", std::move(messageID)},
1181f23b7296SEd Tanous         {"MessageArgs", messageArgs},
1182c4bf6374SJason M. Bills         {"EntryType", "Event"},
118395820184SJason M. Bills         {"Severity", std::move(severity)},
118495820184SJason M. Bills         {"Created", std::move(timestamp)}};
1185c4bf6374SJason M. Bills     return 0;
1186c4bf6374SJason M. Bills }
1187c4bf6374SJason M. Bills 
11887e860f15SJohn Edward Broadbent inline void requestRoutesJournalEventLogEntryCollection(App& app)
1189c4bf6374SJason M. Bills {
11907e860f15SJohn Edward Broadbent     BMCWEB_ROUTE(app,
11917e860f15SJohn Edward Broadbent                  "/redfish/v1/Systems/system/LogServices/EventLog/Entries/")
11928b6a35f0SGunnar Mills         .privileges(redfish::privileges::getLogEntryCollection)
11937e860f15SJohn Edward Broadbent         .methods(boost::beast::http::verb::get)(
11947e860f15SJohn Edward Broadbent             [](const crow::Request& req,
11957e860f15SJohn Edward Broadbent                const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) {
1196271584abSEd Tanous                 uint64_t skip = 0;
1197271584abSEd Tanous                 uint64_t top = maxEntriesPerPage; // Show max entries by default
11988d1b46d7Szhanghch05                 if (!getSkipParam(asyncResp, req, skip))
1199c4bf6374SJason M. Bills                 {
1200c4bf6374SJason M. Bills                     return;
1201c4bf6374SJason M. Bills                 }
12028d1b46d7Szhanghch05                 if (!getTopParam(asyncResp, req, top))
1203c4bf6374SJason M. Bills                 {
1204c4bf6374SJason M. Bills                     return;
1205c4bf6374SJason M. Bills                 }
12067e860f15SJohn Edward Broadbent                 // Collections don't include the static data added by SubRoute
12077e860f15SJohn Edward Broadbent                 // because it has a duplicate entry for members
1208c4bf6374SJason M. Bills                 asyncResp->res.jsonValue["@odata.type"] =
1209c4bf6374SJason M. Bills                     "#LogEntryCollection.LogEntryCollection";
1210c4bf6374SJason M. Bills                 asyncResp->res.jsonValue["@odata.id"] =
1211029573d4SEd Tanous                     "/redfish/v1/Systems/system/LogServices/EventLog/Entries";
1212c4bf6374SJason M. Bills                 asyncResp->res.jsonValue["Name"] = "System Event Log Entries";
1213c4bf6374SJason M. Bills                 asyncResp->res.jsonValue["Description"] =
1214c4bf6374SJason M. Bills                     "Collection of System Event Log Entries";
1215cb92c03bSAndrew Geissler 
12167e860f15SJohn Edward Broadbent                 nlohmann::json& logEntryArray =
12177e860f15SJohn Edward Broadbent                     asyncResp->res.jsonValue["Members"];
1218c4bf6374SJason M. Bills                 logEntryArray = nlohmann::json::array();
12197e860f15SJohn Edward Broadbent                 // Go through the log files and create a unique ID for each
12207e860f15SJohn Edward Broadbent                 // entry
122195820184SJason M. Bills                 std::vector<std::filesystem::path> redfishLogFiles;
122295820184SJason M. Bills                 getRedfishLogFiles(redfishLogFiles);
1223b01bf299SEd Tanous                 uint64_t entryCount = 0;
1224cd225da8SJason M. Bills                 std::string logEntry;
122595820184SJason M. Bills 
12267e860f15SJohn Edward Broadbent                 // Oldest logs are in the last file, so start there and loop
12277e860f15SJohn Edward Broadbent                 // backwards
12287e860f15SJohn Edward Broadbent                 for (auto it = redfishLogFiles.rbegin();
12297e860f15SJohn Edward Broadbent                      it < redfishLogFiles.rend(); it++)
1230c4bf6374SJason M. Bills                 {
1231cd225da8SJason M. Bills                     std::ifstream logStream(*it);
123295820184SJason M. Bills                     if (!logStream.is_open())
1233c4bf6374SJason M. Bills                     {
1234c4bf6374SJason M. Bills                         continue;
1235c4bf6374SJason M. Bills                     }
1236c4bf6374SJason M. Bills 
1237e85d6b16SJason M. Bills                     // Reset the unique ID on the first entry
1238e85d6b16SJason M. Bills                     bool firstEntry = true;
123995820184SJason M. Bills                     while (std::getline(logStream, logEntry))
124095820184SJason M. Bills                     {
1241c4bf6374SJason M. Bills                         entryCount++;
12427e860f15SJohn Edward Broadbent                         // Handle paging using skip (number of entries to skip
12437e860f15SJohn Edward Broadbent                         // from the start) and top (number of entries to
12447e860f15SJohn Edward Broadbent                         // display)
1245c4bf6374SJason M. Bills                         if (entryCount <= skip || entryCount > skip + top)
1246c4bf6374SJason M. Bills                         {
1247c4bf6374SJason M. Bills                             continue;
1248c4bf6374SJason M. Bills                         }
1249c4bf6374SJason M. Bills 
1250c4bf6374SJason M. Bills                         std::string idStr;
1251e85d6b16SJason M. Bills                         if (!getUniqueEntryID(logEntry, idStr, firstEntry))
1252c4bf6374SJason M. Bills                         {
1253c4bf6374SJason M. Bills                             continue;
1254c4bf6374SJason M. Bills                         }
1255c4bf6374SJason M. Bills 
1256e85d6b16SJason M. Bills                         if (firstEntry)
1257e85d6b16SJason M. Bills                         {
1258e85d6b16SJason M. Bills                             firstEntry = false;
1259e85d6b16SJason M. Bills                         }
1260e85d6b16SJason M. Bills 
1261c4bf6374SJason M. Bills                         logEntryArray.push_back({});
1262c4bf6374SJason M. Bills                         nlohmann::json& bmcLogEntry = logEntryArray.back();
12637e860f15SJohn Edward Broadbent                         if (fillEventLogEntryJson(idStr, logEntry,
12647e860f15SJohn Edward Broadbent                                                   bmcLogEntry) != 0)
1265c4bf6374SJason M. Bills                         {
1266c4bf6374SJason M. Bills                             messages::internalError(asyncResp->res);
1267c4bf6374SJason M. Bills                             return;
1268c4bf6374SJason M. Bills                         }
1269c4bf6374SJason M. Bills                     }
127095820184SJason M. Bills                 }
1271c4bf6374SJason M. Bills                 asyncResp->res.jsonValue["Members@odata.count"] = entryCount;
1272c4bf6374SJason M. Bills                 if (skip + top < entryCount)
1273c4bf6374SJason M. Bills                 {
1274c4bf6374SJason M. Bills                     asyncResp->res.jsonValue["Members@odata.nextLink"] =
127595820184SJason M. Bills                         "/redfish/v1/Systems/system/LogServices/EventLog/"
127695820184SJason M. Bills                         "Entries?$skip=" +
1277c4bf6374SJason M. Bills                         std::to_string(skip + top);
1278c4bf6374SJason M. Bills                 }
12797e860f15SJohn Edward Broadbent             });
1280897967deSJason M. Bills }
1281897967deSJason M. Bills 
12827e860f15SJohn Edward Broadbent inline void requestRoutesJournalEventLogEntry(App& app)
1283897967deSJason M. Bills {
12847e860f15SJohn Edward Broadbent     BMCWEB_ROUTE(
12857e860f15SJohn Edward Broadbent         app, "/redfish/v1/Systems/system/LogServices/EventLog/Entries/<str>/")
1286ed398213SEd Tanous         .privileges(redfish::privileges::getLogEntry)
12877e860f15SJohn Edward Broadbent         .methods(boost::beast::http::verb::get)(
12887e860f15SJohn Edward Broadbent             [](const crow::Request&,
12897e860f15SJohn Edward Broadbent                const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
12907e860f15SJohn Edward Broadbent                const std::string& param) {
12917e860f15SJohn Edward Broadbent                 const std::string& targetID = param;
12928d1b46d7Szhanghch05 
12937e860f15SJohn Edward Broadbent                 // Go through the log files and check the unique ID for each
12947e860f15SJohn Edward Broadbent                 // entry to find the target entry
1295897967deSJason M. Bills                 std::vector<std::filesystem::path> redfishLogFiles;
1296897967deSJason M. Bills                 getRedfishLogFiles(redfishLogFiles);
1297897967deSJason M. Bills                 std::string logEntry;
1298897967deSJason M. Bills 
12997e860f15SJohn Edward Broadbent                 // Oldest logs are in the last file, so start there and loop
13007e860f15SJohn Edward Broadbent                 // backwards
13017e860f15SJohn Edward Broadbent                 for (auto it = redfishLogFiles.rbegin();
13027e860f15SJohn Edward Broadbent                      it < redfishLogFiles.rend(); it++)
1303897967deSJason M. Bills                 {
1304897967deSJason M. Bills                     std::ifstream logStream(*it);
1305897967deSJason M. Bills                     if (!logStream.is_open())
1306897967deSJason M. Bills                     {
1307897967deSJason M. Bills                         continue;
1308897967deSJason M. Bills                     }
1309897967deSJason M. Bills 
1310897967deSJason M. Bills                     // Reset the unique ID on the first entry
1311897967deSJason M. Bills                     bool firstEntry = true;
1312897967deSJason M. Bills                     while (std::getline(logStream, logEntry))
1313897967deSJason M. Bills                     {
1314897967deSJason M. Bills                         std::string idStr;
1315897967deSJason M. Bills                         if (!getUniqueEntryID(logEntry, idStr, firstEntry))
1316897967deSJason M. Bills                         {
1317897967deSJason M. Bills                             continue;
1318897967deSJason M. Bills                         }
1319897967deSJason M. Bills 
1320897967deSJason M. Bills                         if (firstEntry)
1321897967deSJason M. Bills                         {
1322897967deSJason M. Bills                             firstEntry = false;
1323897967deSJason M. Bills                         }
1324897967deSJason M. Bills 
1325897967deSJason M. Bills                         if (idStr == targetID)
1326897967deSJason M. Bills                         {
13277e860f15SJohn Edward Broadbent                             if (fillEventLogEntryJson(
13287e860f15SJohn Edward Broadbent                                     idStr, logEntry,
1329897967deSJason M. Bills                                     asyncResp->res.jsonValue) != 0)
1330897967deSJason M. Bills                             {
1331897967deSJason M. Bills                                 messages::internalError(asyncResp->res);
1332897967deSJason M. Bills                                 return;
1333897967deSJason M. Bills                             }
1334897967deSJason M. Bills                             return;
1335897967deSJason M. Bills                         }
1336897967deSJason M. Bills                     }
1337897967deSJason M. Bills                 }
1338897967deSJason M. Bills                 // Requested ID was not found
1339897967deSJason M. Bills                 messages::resourceMissingAtURI(asyncResp->res, targetID);
13407e860f15SJohn Edward Broadbent             });
134108a4e4b5SAnthony Wilson }
134208a4e4b5SAnthony Wilson 
13437e860f15SJohn Edward Broadbent inline void requestRoutesDBusEventLogEntryCollection(App& app)
134408a4e4b5SAnthony Wilson {
13457e860f15SJohn Edward Broadbent     BMCWEB_ROUTE(app,
13467e860f15SJohn Edward Broadbent                  "/redfish/v1/Systems/system/LogServices/EventLog/Entries/")
1347ed398213SEd Tanous         .privileges(redfish::privileges::getLogEntryCollection)
13487e860f15SJohn Edward Broadbent         .methods(
13497e860f15SJohn Edward Broadbent             boost::beast::http::verb::
13507e860f15SJohn Edward Broadbent                 get)([](const crow::Request&,
13517e860f15SJohn Edward Broadbent                         const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) {
13527e860f15SJohn Edward Broadbent             // Collections don't include the static data added by SubRoute
13537e860f15SJohn Edward Broadbent             // because it has a duplicate entry for members
135408a4e4b5SAnthony Wilson             asyncResp->res.jsonValue["@odata.type"] =
135508a4e4b5SAnthony Wilson                 "#LogEntryCollection.LogEntryCollection";
135608a4e4b5SAnthony Wilson             asyncResp->res.jsonValue["@odata.id"] =
135708a4e4b5SAnthony Wilson                 "/redfish/v1/Systems/system/LogServices/EventLog/Entries";
135808a4e4b5SAnthony Wilson             asyncResp->res.jsonValue["Name"] = "System Event Log Entries";
135908a4e4b5SAnthony Wilson             asyncResp->res.jsonValue["Description"] =
136008a4e4b5SAnthony Wilson                 "Collection of System Event Log Entries";
136108a4e4b5SAnthony Wilson 
1362cb92c03bSAndrew Geissler             // DBus implementation of EventLog/Entries
1363cb92c03bSAndrew Geissler             // Make call to Logging Service to find all log entry objects
1364cb92c03bSAndrew Geissler             crow::connections::systemBus->async_method_call(
1365cb92c03bSAndrew Geissler                 [asyncResp](const boost::system::error_code ec,
1366*711ac7a9SEd Tanous                             dbus::utility::ManagedObjectType& resp) {
1367cb92c03bSAndrew Geissler                     if (ec)
1368cb92c03bSAndrew Geissler                     {
1369cb92c03bSAndrew Geissler                         // TODO Handle for specific error code
1370cb92c03bSAndrew Geissler                         BMCWEB_LOG_ERROR
1371cb92c03bSAndrew Geissler                             << "getLogEntriesIfaceData resp_handler got error "
1372cb92c03bSAndrew Geissler                             << ec;
1373cb92c03bSAndrew Geissler                         messages::internalError(asyncResp->res);
1374cb92c03bSAndrew Geissler                         return;
1375cb92c03bSAndrew Geissler                     }
1376cb92c03bSAndrew Geissler                     nlohmann::json& entriesArray =
1377cb92c03bSAndrew Geissler                         asyncResp->res.jsonValue["Members"];
1378cb92c03bSAndrew Geissler                     entriesArray = nlohmann::json::array();
1379cb92c03bSAndrew Geissler                     for (auto& objectPath : resp)
1380cb92c03bSAndrew Geissler                     {
138166664f25SEd Tanous                         uint32_t* id = nullptr;
138266664f25SEd Tanous                         std::time_t timestamp{};
1383d139c236SGeorge Liu                         std::time_t updateTimestamp{};
138466664f25SEd Tanous                         std::string* severity = nullptr;
138566664f25SEd Tanous                         std::string* message = nullptr;
1386f86bb901SAdriana Kobylak                         std::string* filePath = nullptr;
138775710de2SXiaochao Ma                         bool resolved = false;
1388f86bb901SAdriana Kobylak                         for (auto& interfaceMap : objectPath.second)
1389f86bb901SAdriana Kobylak                         {
1390f86bb901SAdriana Kobylak                             if (interfaceMap.first ==
1391f86bb901SAdriana Kobylak                                 "xyz.openbmc_project.Logging.Entry")
1392f86bb901SAdriana Kobylak                             {
1393cb92c03bSAndrew Geissler                                 for (auto& propertyMap : interfaceMap.second)
1394cb92c03bSAndrew Geissler                                 {
1395cb92c03bSAndrew Geissler                                     if (propertyMap.first == "Id")
1396cb92c03bSAndrew Geissler                                     {
1397f86bb901SAdriana Kobylak                                         id = std::get_if<uint32_t>(
1398f86bb901SAdriana Kobylak                                             &propertyMap.second);
1399cb92c03bSAndrew Geissler                                     }
1400cb92c03bSAndrew Geissler                                     else if (propertyMap.first == "Timestamp")
1401cb92c03bSAndrew Geissler                                     {
1402cb92c03bSAndrew Geissler                                         const uint64_t* millisTimeStamp =
1403f86bb901SAdriana Kobylak                                             std::get_if<uint64_t>(
1404f86bb901SAdriana Kobylak                                                 &propertyMap.second);
1405ae34c8e8SAdriana Kobylak                                         if (millisTimeStamp != nullptr)
1406ebd45906SGeorge Liu                                         {
14077e860f15SJohn Edward Broadbent                                             timestamp =
14087e860f15SJohn Edward Broadbent                                                 crow::utility::getTimestamp(
14097e860f15SJohn Edward Broadbent                                                     *millisTimeStamp);
14107e860f15SJohn Edward Broadbent                                         }
14117e860f15SJohn Edward Broadbent                                     }
14127e860f15SJohn Edward Broadbent                                     else if (propertyMap.first ==
14137e860f15SJohn Edward Broadbent                                              "UpdateTimestamp")
14147e860f15SJohn Edward Broadbent                                     {
14157e860f15SJohn Edward Broadbent                                         const uint64_t* millisTimeStamp =
14167e860f15SJohn Edward Broadbent                                             std::get_if<uint64_t>(
14177e860f15SJohn Edward Broadbent                                                 &propertyMap.second);
14187e860f15SJohn Edward Broadbent                                         if (millisTimeStamp != nullptr)
14197e860f15SJohn Edward Broadbent                                         {
14207e860f15SJohn Edward Broadbent                                             updateTimestamp =
14217e860f15SJohn Edward Broadbent                                                 crow::utility::getTimestamp(
14227e860f15SJohn Edward Broadbent                                                     *millisTimeStamp);
14237e860f15SJohn Edward Broadbent                                         }
14247e860f15SJohn Edward Broadbent                                     }
14257e860f15SJohn Edward Broadbent                                     else if (propertyMap.first == "Severity")
14267e860f15SJohn Edward Broadbent                                     {
14277e860f15SJohn Edward Broadbent                                         severity = std::get_if<std::string>(
14287e860f15SJohn Edward Broadbent                                             &propertyMap.second);
14297e860f15SJohn Edward Broadbent                                     }
14307e860f15SJohn Edward Broadbent                                     else if (propertyMap.first == "Message")
14317e860f15SJohn Edward Broadbent                                     {
14327e860f15SJohn Edward Broadbent                                         message = std::get_if<std::string>(
14337e860f15SJohn Edward Broadbent                                             &propertyMap.second);
14347e860f15SJohn Edward Broadbent                                     }
14357e860f15SJohn Edward Broadbent                                     else if (propertyMap.first == "Resolved")
14367e860f15SJohn Edward Broadbent                                     {
14377e860f15SJohn Edward Broadbent                                         bool* resolveptr = std::get_if<bool>(
14387e860f15SJohn Edward Broadbent                                             &propertyMap.second);
14397e860f15SJohn Edward Broadbent                                         if (resolveptr == nullptr)
14407e860f15SJohn Edward Broadbent                                         {
14417e860f15SJohn Edward Broadbent                                             messages::internalError(
14427e860f15SJohn Edward Broadbent                                                 asyncResp->res);
14437e860f15SJohn Edward Broadbent                                             return;
14447e860f15SJohn Edward Broadbent                                         }
14457e860f15SJohn Edward Broadbent                                         resolved = *resolveptr;
14467e860f15SJohn Edward Broadbent                                     }
14477e860f15SJohn Edward Broadbent                                 }
14487e860f15SJohn Edward Broadbent                                 if (id == nullptr || message == nullptr ||
14497e860f15SJohn Edward Broadbent                                     severity == nullptr)
14507e860f15SJohn Edward Broadbent                                 {
14517e860f15SJohn Edward Broadbent                                     messages::internalError(asyncResp->res);
14527e860f15SJohn Edward Broadbent                                     return;
14537e860f15SJohn Edward Broadbent                                 }
14547e860f15SJohn Edward Broadbent                             }
14557e860f15SJohn Edward Broadbent                             else if (interfaceMap.first ==
14567e860f15SJohn Edward Broadbent                                      "xyz.openbmc_project.Common.FilePath")
14577e860f15SJohn Edward Broadbent                             {
14587e860f15SJohn Edward Broadbent                                 for (auto& propertyMap : interfaceMap.second)
14597e860f15SJohn Edward Broadbent                                 {
14607e860f15SJohn Edward Broadbent                                     if (propertyMap.first == "Path")
14617e860f15SJohn Edward Broadbent                                     {
14627e860f15SJohn Edward Broadbent                                         filePath = std::get_if<std::string>(
14637e860f15SJohn Edward Broadbent                                             &propertyMap.second);
14647e860f15SJohn Edward Broadbent                                     }
14657e860f15SJohn Edward Broadbent                                 }
14667e860f15SJohn Edward Broadbent                             }
14677e860f15SJohn Edward Broadbent                         }
14687e860f15SJohn Edward Broadbent                         // Object path without the
14697e860f15SJohn Edward Broadbent                         // xyz.openbmc_project.Logging.Entry interface, ignore
14707e860f15SJohn Edward Broadbent                         // and continue.
14717e860f15SJohn Edward Broadbent                         if (id == nullptr || message == nullptr ||
14727e860f15SJohn Edward Broadbent                             severity == nullptr)
14737e860f15SJohn Edward Broadbent                         {
14747e860f15SJohn Edward Broadbent                             continue;
14757e860f15SJohn Edward Broadbent                         }
14767e860f15SJohn Edward Broadbent                         entriesArray.push_back({});
14777e860f15SJohn Edward Broadbent                         nlohmann::json& thisEntry = entriesArray.back();
14787e860f15SJohn Edward Broadbent                         thisEntry["@odata.type"] = "#LogEntry.v1_8_0.LogEntry";
14797e860f15SJohn Edward Broadbent                         thisEntry["@odata.id"] =
14800fda0f12SGeorge Liu                             "/redfish/v1/Systems/system/LogServices/EventLog/Entries/" +
14817e860f15SJohn Edward Broadbent                             std::to_string(*id);
14827e860f15SJohn Edward Broadbent                         thisEntry["Name"] = "System Event Log Entry";
14837e860f15SJohn Edward Broadbent                         thisEntry["Id"] = std::to_string(*id);
14847e860f15SJohn Edward Broadbent                         thisEntry["Message"] = *message;
14857e860f15SJohn Edward Broadbent                         thisEntry["Resolved"] = resolved;
14867e860f15SJohn Edward Broadbent                         thisEntry["EntryType"] = "Event";
14877e860f15SJohn Edward Broadbent                         thisEntry["Severity"] =
14887e860f15SJohn Edward Broadbent                             translateSeverityDbusToRedfish(*severity);
14897e860f15SJohn Edward Broadbent                         thisEntry["Created"] =
14901d8782e7SNan Zhou                             crow::utility::getDateTimeStdtime(timestamp);
14917e860f15SJohn Edward Broadbent                         thisEntry["Modified"] =
14921d8782e7SNan Zhou                             crow::utility::getDateTimeStdtime(updateTimestamp);
14937e860f15SJohn Edward Broadbent                         if (filePath != nullptr)
14947e860f15SJohn Edward Broadbent                         {
14957e860f15SJohn Edward Broadbent                             thisEntry["AdditionalDataURI"] =
14960fda0f12SGeorge Liu                                 "/redfish/v1/Systems/system/LogServices/EventLog/Entries/" +
14977e860f15SJohn Edward Broadbent                                 std::to_string(*id) + "/attachment";
14987e860f15SJohn Edward Broadbent                         }
14997e860f15SJohn Edward Broadbent                     }
15007e860f15SJohn Edward Broadbent                     std::sort(entriesArray.begin(), entriesArray.end(),
15017e860f15SJohn Edward Broadbent                               [](const nlohmann::json& left,
15027e860f15SJohn Edward Broadbent                                  const nlohmann::json& right) {
15037e860f15SJohn Edward Broadbent                                   return (left["Id"] <= right["Id"]);
15047e860f15SJohn Edward Broadbent                               });
15057e860f15SJohn Edward Broadbent                     asyncResp->res.jsonValue["Members@odata.count"] =
15067e860f15SJohn Edward Broadbent                         entriesArray.size();
15077e860f15SJohn Edward Broadbent                 },
15087e860f15SJohn Edward Broadbent                 "xyz.openbmc_project.Logging", "/xyz/openbmc_project/logging",
15097e860f15SJohn Edward Broadbent                 "org.freedesktop.DBus.ObjectManager", "GetManagedObjects");
15107e860f15SJohn Edward Broadbent         });
15117e860f15SJohn Edward Broadbent }
15127e860f15SJohn Edward Broadbent 
15137e860f15SJohn Edward Broadbent inline void requestRoutesDBusEventLogEntry(App& app)
15147e860f15SJohn Edward Broadbent {
15157e860f15SJohn Edward Broadbent     BMCWEB_ROUTE(
15167e860f15SJohn Edward Broadbent         app, "/redfish/v1/Systems/system/LogServices/EventLog/Entries/<str>/")
1517ed398213SEd Tanous         .privileges(redfish::privileges::getLogEntry)
15187e860f15SJohn Edward Broadbent         .methods(boost::beast::http::verb::get)(
15197e860f15SJohn Edward Broadbent             [](const crow::Request&,
15207e860f15SJohn Edward Broadbent                const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
15217e860f15SJohn Edward Broadbent                const std::string& param)
15227e860f15SJohn Edward Broadbent 
15237e860f15SJohn Edward Broadbent             {
15247e860f15SJohn Edward Broadbent                 std::string entryID = param;
15257e860f15SJohn Edward Broadbent                 dbus::utility::escapePathForDbus(entryID);
15267e860f15SJohn Edward Broadbent 
15277e860f15SJohn Edward Broadbent                 // DBus implementation of EventLog/Entries
15287e860f15SJohn Edward Broadbent                 // Make call to Logging Service to find all log entry objects
15297e860f15SJohn Edward Broadbent                 crow::connections::systemBus->async_method_call(
15307e860f15SJohn Edward Broadbent                     [asyncResp, entryID](const boost::system::error_code ec,
15317e860f15SJohn Edward Broadbent                                          GetManagedPropertyType& resp) {
15327e860f15SJohn Edward Broadbent                         if (ec.value() == EBADR)
15337e860f15SJohn Edward Broadbent                         {
15347e860f15SJohn Edward Broadbent                             messages::resourceNotFound(
15357e860f15SJohn Edward Broadbent                                 asyncResp->res, "EventLogEntry", entryID);
15367e860f15SJohn Edward Broadbent                             return;
15377e860f15SJohn Edward Broadbent                         }
15387e860f15SJohn Edward Broadbent                         if (ec)
15397e860f15SJohn Edward Broadbent                         {
15400fda0f12SGeorge Liu                             BMCWEB_LOG_ERROR
15410fda0f12SGeorge Liu                                 << "EventLogEntry (DBus) resp_handler got error "
15427e860f15SJohn Edward Broadbent                                 << ec;
15437e860f15SJohn Edward Broadbent                             messages::internalError(asyncResp->res);
15447e860f15SJohn Edward Broadbent                             return;
15457e860f15SJohn Edward Broadbent                         }
15467e860f15SJohn Edward Broadbent                         uint32_t* id = nullptr;
15477e860f15SJohn Edward Broadbent                         std::time_t timestamp{};
15487e860f15SJohn Edward Broadbent                         std::time_t updateTimestamp{};
15497e860f15SJohn Edward Broadbent                         std::string* severity = nullptr;
15507e860f15SJohn Edward Broadbent                         std::string* message = nullptr;
15517e860f15SJohn Edward Broadbent                         std::string* filePath = nullptr;
15527e860f15SJohn Edward Broadbent                         bool resolved = false;
15537e860f15SJohn Edward Broadbent 
15547e860f15SJohn Edward Broadbent                         for (auto& propertyMap : resp)
15557e860f15SJohn Edward Broadbent                         {
15567e860f15SJohn Edward Broadbent                             if (propertyMap.first == "Id")
15577e860f15SJohn Edward Broadbent                             {
15587e860f15SJohn Edward Broadbent                                 id = std::get_if<uint32_t>(&propertyMap.second);
15597e860f15SJohn Edward Broadbent                             }
15607e860f15SJohn Edward Broadbent                             else if (propertyMap.first == "Timestamp")
15617e860f15SJohn Edward Broadbent                             {
15627e860f15SJohn Edward Broadbent                                 const uint64_t* millisTimeStamp =
15637e860f15SJohn Edward Broadbent                                     std::get_if<uint64_t>(&propertyMap.second);
15647e860f15SJohn Edward Broadbent                                 if (millisTimeStamp != nullptr)
15657e860f15SJohn Edward Broadbent                                 {
1566d139c236SGeorge Liu                                     timestamp = crow::utility::getTimestamp(
1567cb92c03bSAndrew Geissler                                         *millisTimeStamp);
1568d139c236SGeorge Liu                                 }
1569ebd45906SGeorge Liu                             }
1570d139c236SGeorge Liu                             else if (propertyMap.first == "UpdateTimestamp")
1571d139c236SGeorge Liu                             {
1572d139c236SGeorge Liu                                 const uint64_t* millisTimeStamp =
15737e860f15SJohn Edward Broadbent                                     std::get_if<uint64_t>(&propertyMap.second);
1574ae34c8e8SAdriana Kobylak                                 if (millisTimeStamp != nullptr)
1575ebd45906SGeorge Liu                                 {
1576ebd45906SGeorge Liu                                     updateTimestamp =
1577ebd45906SGeorge Liu                                         crow::utility::getTimestamp(
1578d139c236SGeorge Liu                                             *millisTimeStamp);
1579cb92c03bSAndrew Geissler                                 }
1580ebd45906SGeorge Liu                             }
1581cb92c03bSAndrew Geissler                             else if (propertyMap.first == "Severity")
1582cb92c03bSAndrew Geissler                             {
1583cb92c03bSAndrew Geissler                                 severity = std::get_if<std::string>(
1584cb92c03bSAndrew Geissler                                     &propertyMap.second);
1585cb92c03bSAndrew Geissler                             }
1586cb92c03bSAndrew Geissler                             else if (propertyMap.first == "Message")
1587cb92c03bSAndrew Geissler                             {
1588cb92c03bSAndrew Geissler                                 message = std::get_if<std::string>(
1589cb92c03bSAndrew Geissler                                     &propertyMap.second);
1590ae34c8e8SAdriana Kobylak                             }
159175710de2SXiaochao Ma                             else if (propertyMap.first == "Resolved")
159275710de2SXiaochao Ma                             {
159375710de2SXiaochao Ma                                 bool* resolveptr =
159475710de2SXiaochao Ma                                     std::get_if<bool>(&propertyMap.second);
159575710de2SXiaochao Ma                                 if (resolveptr == nullptr)
159675710de2SXiaochao Ma                                 {
159775710de2SXiaochao Ma                                     messages::internalError(asyncResp->res);
159875710de2SXiaochao Ma                                     return;
159975710de2SXiaochao Ma                                 }
160075710de2SXiaochao Ma                                 resolved = *resolveptr;
160175710de2SXiaochao Ma                             }
16027e860f15SJohn Edward Broadbent                             else if (propertyMap.first == "Path")
1603f86bb901SAdriana Kobylak                             {
1604f86bb901SAdriana Kobylak                                 filePath = std::get_if<std::string>(
1605f86bb901SAdriana Kobylak                                     &propertyMap.second);
1606f86bb901SAdriana Kobylak                             }
1607f86bb901SAdriana Kobylak                         }
1608f86bb901SAdriana Kobylak                         if (id == nullptr || message == nullptr ||
1609f86bb901SAdriana Kobylak                             severity == nullptr)
1610f86bb901SAdriana Kobylak                         {
1611ae34c8e8SAdriana Kobylak                             messages::internalError(asyncResp->res);
1612271584abSEd Tanous                             return;
1613271584abSEd Tanous                         }
1614f86bb901SAdriana Kobylak                         asyncResp->res.jsonValue["@odata.type"] =
1615f86bb901SAdriana Kobylak                             "#LogEntry.v1_8_0.LogEntry";
1616f86bb901SAdriana Kobylak                         asyncResp->res.jsonValue["@odata.id"] =
16170fda0f12SGeorge Liu                             "/redfish/v1/Systems/system/LogServices/EventLog/Entries/" +
1618f86bb901SAdriana Kobylak                             std::to_string(*id);
16197e860f15SJohn Edward Broadbent                         asyncResp->res.jsonValue["Name"] =
16207e860f15SJohn Edward Broadbent                             "System Event Log Entry";
1621f86bb901SAdriana Kobylak                         asyncResp->res.jsonValue["Id"] = std::to_string(*id);
1622f86bb901SAdriana Kobylak                         asyncResp->res.jsonValue["Message"] = *message;
1623f86bb901SAdriana Kobylak                         asyncResp->res.jsonValue["Resolved"] = resolved;
1624f86bb901SAdriana Kobylak                         asyncResp->res.jsonValue["EntryType"] = "Event";
1625f86bb901SAdriana Kobylak                         asyncResp->res.jsonValue["Severity"] =
1626f86bb901SAdriana Kobylak                             translateSeverityDbusToRedfish(*severity);
1627f86bb901SAdriana Kobylak                         asyncResp->res.jsonValue["Created"] =
16281d8782e7SNan Zhou                             crow::utility::getDateTimeStdtime(timestamp);
1629f86bb901SAdriana Kobylak                         asyncResp->res.jsonValue["Modified"] =
16301d8782e7SNan Zhou                             crow::utility::getDateTimeStdtime(updateTimestamp);
1631f86bb901SAdriana Kobylak                         if (filePath != nullptr)
1632f86bb901SAdriana Kobylak                         {
1633f86bb901SAdriana Kobylak                             asyncResp->res.jsonValue["AdditionalDataURI"] =
16340fda0f12SGeorge Liu                                 "/redfish/v1/Systems/system/LogServices/EventLog/attachment/" +
16357e860f15SJohn Edward Broadbent                                 std::to_string(*id);
1636f86bb901SAdriana Kobylak                         }
1637cb92c03bSAndrew Geissler                     },
1638cb92c03bSAndrew Geissler                     "xyz.openbmc_project.Logging",
1639cb92c03bSAndrew Geissler                     "/xyz/openbmc_project/logging/entry/" + entryID,
1640f86bb901SAdriana Kobylak                     "org.freedesktop.DBus.Properties", "GetAll", "");
16417e860f15SJohn Edward Broadbent             });
1642336e96c6SChicago Duan 
16437e860f15SJohn Edward Broadbent     BMCWEB_ROUTE(
16447e860f15SJohn Edward Broadbent         app, "/redfish/v1/Systems/system/LogServices/EventLog/Entries/<str>/")
1645ed398213SEd Tanous         .privileges(redfish::privileges::patchLogEntry)
16467e860f15SJohn Edward Broadbent         .methods(boost::beast::http::verb::patch)(
16477e860f15SJohn Edward Broadbent             [](const crow::Request& req,
16487e860f15SJohn Edward Broadbent                const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
16497e860f15SJohn Edward Broadbent                const std::string& entryId) {
165075710de2SXiaochao Ma                 std::optional<bool> resolved;
165175710de2SXiaochao Ma 
16527e860f15SJohn Edward Broadbent                 if (!json_util::readJson(req, asyncResp->res, "Resolved",
16537e860f15SJohn Edward Broadbent                                          resolved))
165475710de2SXiaochao Ma                 {
165575710de2SXiaochao Ma                     return;
165675710de2SXiaochao Ma                 }
165775710de2SXiaochao Ma                 BMCWEB_LOG_DEBUG << "Set Resolved";
165875710de2SXiaochao Ma 
165975710de2SXiaochao Ma                 crow::connections::systemBus->async_method_call(
16604f48d5f6SEd Tanous                     [asyncResp, entryId](const boost::system::error_code ec) {
166175710de2SXiaochao Ma                         if (ec)
166275710de2SXiaochao Ma                         {
166375710de2SXiaochao Ma                             BMCWEB_LOG_DEBUG << "DBUS response error " << ec;
166475710de2SXiaochao Ma                             messages::internalError(asyncResp->res);
166575710de2SXiaochao Ma                             return;
166675710de2SXiaochao Ma                         }
166775710de2SXiaochao Ma                     },
166875710de2SXiaochao Ma                     "xyz.openbmc_project.Logging",
166975710de2SXiaochao Ma                     "/xyz/openbmc_project/logging/entry/" + entryId,
167075710de2SXiaochao Ma                     "org.freedesktop.DBus.Properties", "Set",
167175710de2SXiaochao Ma                     "xyz.openbmc_project.Logging.Entry", "Resolved",
1672168e20c1SEd Tanous                     dbus::utility::DbusVariantType(*resolved));
16737e860f15SJohn Edward Broadbent             });
167475710de2SXiaochao Ma 
16757e860f15SJohn Edward Broadbent     BMCWEB_ROUTE(
16767e860f15SJohn Edward Broadbent         app, "/redfish/v1/Systems/system/LogServices/EventLog/Entries/<str>/")
1677ed398213SEd Tanous         .privileges(redfish::privileges::deleteLogEntry)
1678ed398213SEd Tanous 
16797e860f15SJohn Edward Broadbent         .methods(boost::beast::http::verb::delete_)(
16807e860f15SJohn Edward Broadbent             [](const crow::Request&,
16817e860f15SJohn Edward Broadbent                const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
16827e860f15SJohn Edward Broadbent                const std::string& param)
16837e860f15SJohn Edward Broadbent 
1684336e96c6SChicago Duan             {
1685336e96c6SChicago Duan                 BMCWEB_LOG_DEBUG << "Do delete single event entries.";
1686336e96c6SChicago Duan 
16877e860f15SJohn Edward Broadbent                 std::string entryID = param;
1688336e96c6SChicago Duan 
1689336e96c6SChicago Duan                 dbus::utility::escapePathForDbus(entryID);
1690336e96c6SChicago Duan 
1691336e96c6SChicago Duan                 // Process response from Logging service.
16927e860f15SJohn Edward Broadbent                 auto respHandler = [asyncResp, entryID](
16937e860f15SJohn Edward Broadbent                                        const boost::system::error_code ec) {
16947e860f15SJohn Edward Broadbent                     BMCWEB_LOG_DEBUG
16957e860f15SJohn Edward Broadbent                         << "EventLogEntry (DBus) doDelete callback: Done";
1696336e96c6SChicago Duan                     if (ec)
1697336e96c6SChicago Duan                     {
16983de8d8baSGeorge Liu                         if (ec.value() == EBADR)
16993de8d8baSGeorge Liu                         {
17007e860f15SJohn Edward Broadbent                             messages::resourceNotFound(asyncResp->res,
17017e860f15SJohn Edward Broadbent                                                        "LogEntry", entryID);
17023de8d8baSGeorge Liu                             return;
17033de8d8baSGeorge Liu                         }
1704336e96c6SChicago Duan                         // TODO Handle for specific error code
17050fda0f12SGeorge Liu                         BMCWEB_LOG_ERROR
17060fda0f12SGeorge Liu                             << "EventLogEntry (DBus) doDelete respHandler got error "
1707336e96c6SChicago Duan                             << ec;
1708336e96c6SChicago Duan                         asyncResp->res.result(
1709336e96c6SChicago Duan                             boost::beast::http::status::internal_server_error);
1710336e96c6SChicago Duan                         return;
1711336e96c6SChicago Duan                     }
1712336e96c6SChicago Duan 
1713336e96c6SChicago Duan                     asyncResp->res.result(boost::beast::http::status::ok);
1714336e96c6SChicago Duan                 };
1715336e96c6SChicago Duan 
1716336e96c6SChicago Duan                 // Make call to Logging service to request Delete Log
1717336e96c6SChicago Duan                 crow::connections::systemBus->async_method_call(
1718336e96c6SChicago Duan                     respHandler, "xyz.openbmc_project.Logging",
1719336e96c6SChicago Duan                     "/xyz/openbmc_project/logging/entry/" + entryID,
1720336e96c6SChicago Duan                     "xyz.openbmc_project.Object.Delete", "Delete");
17217e860f15SJohn Edward Broadbent             });
1722400fd1fbSAdriana Kobylak }
1723400fd1fbSAdriana Kobylak 
17247e860f15SJohn Edward Broadbent inline void requestRoutesDBusEventLogEntryDownload(App& app)
1725400fd1fbSAdriana Kobylak {
17260fda0f12SGeorge Liu     BMCWEB_ROUTE(
17270fda0f12SGeorge Liu         app,
17280fda0f12SGeorge Liu         "/redfish/v1/Systems/system/LogServices/EventLog/Entries/<str>/attachment")
1729ed398213SEd Tanous         .privileges(redfish::privileges::getLogEntry)
17307e860f15SJohn Edward Broadbent         .methods(boost::beast::http::verb::get)(
17317e860f15SJohn Edward Broadbent             [](const crow::Request& req,
17327e860f15SJohn Edward Broadbent                const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
17337e860f15SJohn Edward Broadbent                const std::string& param)
1734400fd1fbSAdriana Kobylak 
17357e860f15SJohn Edward Broadbent             {
1736647b3cdcSGeorge Liu                 if (!http_helpers::isOctetAccepted(
1737647b3cdcSGeorge Liu                         req.getHeaderValue("Accept")))
1738400fd1fbSAdriana Kobylak                 {
17397e860f15SJohn Edward Broadbent                     asyncResp->res.result(
17407e860f15SJohn Edward Broadbent                         boost::beast::http::status::bad_request);
1741400fd1fbSAdriana Kobylak                     return;
1742400fd1fbSAdriana Kobylak                 }
1743400fd1fbSAdriana Kobylak 
17447e860f15SJohn Edward Broadbent                 std::string entryID = param;
1745400fd1fbSAdriana Kobylak                 dbus::utility::escapePathForDbus(entryID);
1746400fd1fbSAdriana Kobylak 
1747400fd1fbSAdriana Kobylak                 crow::connections::systemBus->async_method_call(
17487e860f15SJohn Edward Broadbent                     [asyncResp,
17497e860f15SJohn Edward Broadbent                      entryID](const boost::system::error_code ec,
1750400fd1fbSAdriana Kobylak                               const sdbusplus::message::unix_fd& unixfd) {
1751400fd1fbSAdriana Kobylak                         if (ec.value() == EBADR)
1752400fd1fbSAdriana Kobylak                         {
17537e860f15SJohn Edward Broadbent                             messages::resourceNotFound(
17547e860f15SJohn Edward Broadbent                                 asyncResp->res, "EventLogAttachment", entryID);
1755400fd1fbSAdriana Kobylak                             return;
1756400fd1fbSAdriana Kobylak                         }
1757400fd1fbSAdriana Kobylak                         if (ec)
1758400fd1fbSAdriana Kobylak                         {
1759400fd1fbSAdriana Kobylak                             BMCWEB_LOG_DEBUG << "DBUS response error " << ec;
1760400fd1fbSAdriana Kobylak                             messages::internalError(asyncResp->res);
1761400fd1fbSAdriana Kobylak                             return;
1762400fd1fbSAdriana Kobylak                         }
1763400fd1fbSAdriana Kobylak 
1764400fd1fbSAdriana Kobylak                         int fd = -1;
1765400fd1fbSAdriana Kobylak                         fd = dup(unixfd);
1766400fd1fbSAdriana Kobylak                         if (fd == -1)
1767400fd1fbSAdriana Kobylak                         {
1768400fd1fbSAdriana Kobylak                             messages::internalError(asyncResp->res);
1769400fd1fbSAdriana Kobylak                             return;
1770400fd1fbSAdriana Kobylak                         }
1771400fd1fbSAdriana Kobylak 
1772400fd1fbSAdriana Kobylak                         long long int size = lseek(fd, 0, SEEK_END);
1773400fd1fbSAdriana Kobylak                         if (size == -1)
1774400fd1fbSAdriana Kobylak                         {
1775400fd1fbSAdriana Kobylak                             messages::internalError(asyncResp->res);
1776400fd1fbSAdriana Kobylak                             return;
1777400fd1fbSAdriana Kobylak                         }
1778400fd1fbSAdriana Kobylak 
1779400fd1fbSAdriana Kobylak                         // Arbitrary max size of 64kb
1780400fd1fbSAdriana Kobylak                         constexpr int maxFileSize = 65536;
1781400fd1fbSAdriana Kobylak                         if (size > maxFileSize)
1782400fd1fbSAdriana Kobylak                         {
1783400fd1fbSAdriana Kobylak                             BMCWEB_LOG_ERROR
1784400fd1fbSAdriana Kobylak                                 << "File size exceeds maximum allowed size of "
1785400fd1fbSAdriana Kobylak                                 << maxFileSize;
1786400fd1fbSAdriana Kobylak                             messages::internalError(asyncResp->res);
1787400fd1fbSAdriana Kobylak                             return;
1788400fd1fbSAdriana Kobylak                         }
1789400fd1fbSAdriana Kobylak                         std::vector<char> data(static_cast<size_t>(size));
1790400fd1fbSAdriana Kobylak                         long long int rc = lseek(fd, 0, SEEK_SET);
1791400fd1fbSAdriana Kobylak                         if (rc == -1)
1792400fd1fbSAdriana Kobylak                         {
1793400fd1fbSAdriana Kobylak                             messages::internalError(asyncResp->res);
1794400fd1fbSAdriana Kobylak                             return;
1795400fd1fbSAdriana Kobylak                         }
1796400fd1fbSAdriana Kobylak                         rc = read(fd, data.data(), data.size());
1797400fd1fbSAdriana Kobylak                         if ((rc == -1) || (rc != size))
1798400fd1fbSAdriana Kobylak                         {
1799400fd1fbSAdriana Kobylak                             messages::internalError(asyncResp->res);
1800400fd1fbSAdriana Kobylak                             return;
1801400fd1fbSAdriana Kobylak                         }
1802400fd1fbSAdriana Kobylak                         close(fd);
1803400fd1fbSAdriana Kobylak 
1804400fd1fbSAdriana Kobylak                         std::string_view strData(data.data(), data.size());
18057e860f15SJohn Edward Broadbent                         std::string output =
18067e860f15SJohn Edward Broadbent                             crow::utility::base64encode(strData);
1807400fd1fbSAdriana Kobylak 
1808400fd1fbSAdriana Kobylak                         asyncResp->res.addHeader("Content-Type",
1809400fd1fbSAdriana Kobylak                                                  "application/octet-stream");
18107e860f15SJohn Edward Broadbent                         asyncResp->res.addHeader("Content-Transfer-Encoding",
18117e860f15SJohn Edward Broadbent                                                  "Base64");
1812400fd1fbSAdriana Kobylak                         asyncResp->res.body() = std::move(output);
1813400fd1fbSAdriana Kobylak                     },
1814400fd1fbSAdriana Kobylak                     "xyz.openbmc_project.Logging",
1815400fd1fbSAdriana Kobylak                     "/xyz/openbmc_project/logging/entry/" + entryID,
1816400fd1fbSAdriana Kobylak                     "xyz.openbmc_project.Logging.Entry", "GetEntry");
18177e860f15SJohn Edward Broadbent             });
18181da66f75SEd Tanous }
18191da66f75SEd Tanous 
1820b7028ebfSSpencer Ku constexpr const char* hostLoggerFolderPath = "/var/log/console";
1821b7028ebfSSpencer Ku 
1822b7028ebfSSpencer Ku inline bool
1823b7028ebfSSpencer Ku     getHostLoggerFiles(const std::string& hostLoggerFilePath,
1824b7028ebfSSpencer Ku                        std::vector<std::filesystem::path>& hostLoggerFiles)
1825b7028ebfSSpencer Ku {
1826b7028ebfSSpencer Ku     std::error_code ec;
1827b7028ebfSSpencer Ku     std::filesystem::directory_iterator logPath(hostLoggerFilePath, ec);
1828b7028ebfSSpencer Ku     if (ec)
1829b7028ebfSSpencer Ku     {
1830b7028ebfSSpencer Ku         BMCWEB_LOG_ERROR << ec.message();
1831b7028ebfSSpencer Ku         return false;
1832b7028ebfSSpencer Ku     }
1833b7028ebfSSpencer Ku     for (const std::filesystem::directory_entry& it : logPath)
1834b7028ebfSSpencer Ku     {
1835b7028ebfSSpencer Ku         std::string filename = it.path().filename();
1836b7028ebfSSpencer Ku         // Prefix of each log files is "log". Find the file and save the
1837b7028ebfSSpencer Ku         // path
1838b7028ebfSSpencer Ku         if (boost::starts_with(filename, "log"))
1839b7028ebfSSpencer Ku         {
1840b7028ebfSSpencer Ku             hostLoggerFiles.emplace_back(it.path());
1841b7028ebfSSpencer Ku         }
1842b7028ebfSSpencer Ku     }
1843b7028ebfSSpencer Ku     // As the log files rotate, they are appended with a ".#" that is higher for
1844b7028ebfSSpencer Ku     // the older logs. Since we start from oldest logs, sort the name in
1845b7028ebfSSpencer Ku     // descending order.
1846b7028ebfSSpencer Ku     std::sort(hostLoggerFiles.rbegin(), hostLoggerFiles.rend(),
1847b7028ebfSSpencer Ku               AlphanumLess<std::string>());
1848b7028ebfSSpencer Ku 
1849b7028ebfSSpencer Ku     return true;
1850b7028ebfSSpencer Ku }
1851b7028ebfSSpencer Ku 
1852b7028ebfSSpencer Ku inline bool
1853b7028ebfSSpencer Ku     getHostLoggerEntries(std::vector<std::filesystem::path>& hostLoggerFiles,
1854b7028ebfSSpencer Ku                          uint64_t& skip, uint64_t& top,
1855b7028ebfSSpencer Ku                          std::vector<std::string>& logEntries, size_t& logCount)
1856b7028ebfSSpencer Ku {
1857b7028ebfSSpencer Ku     GzFileReader logFile;
1858b7028ebfSSpencer Ku 
1859b7028ebfSSpencer Ku     // Go though all log files and expose host logs.
1860b7028ebfSSpencer Ku     for (const std::filesystem::path& it : hostLoggerFiles)
1861b7028ebfSSpencer Ku     {
1862b7028ebfSSpencer Ku         if (!logFile.gzGetLines(it.string(), skip, top, logEntries, logCount))
1863b7028ebfSSpencer Ku         {
1864b7028ebfSSpencer Ku             BMCWEB_LOG_ERROR << "fail to expose host logs";
1865b7028ebfSSpencer Ku             return false;
1866b7028ebfSSpencer Ku         }
1867b7028ebfSSpencer Ku     }
1868b7028ebfSSpencer Ku     // Get lastMessage from constructor by getter
1869b7028ebfSSpencer Ku     std::string lastMessage = logFile.getLastMessage();
1870b7028ebfSSpencer Ku     if (!lastMessage.empty())
1871b7028ebfSSpencer Ku     {
1872b7028ebfSSpencer Ku         logCount++;
1873b7028ebfSSpencer Ku         if (logCount > skip && logCount <= (skip + top))
1874b7028ebfSSpencer Ku         {
1875b7028ebfSSpencer Ku             logEntries.push_back(lastMessage);
1876b7028ebfSSpencer Ku         }
1877b7028ebfSSpencer Ku     }
1878b7028ebfSSpencer Ku     return true;
1879b7028ebfSSpencer Ku }
1880b7028ebfSSpencer Ku 
1881b7028ebfSSpencer Ku inline void fillHostLoggerEntryJson(const std::string& logEntryID,
1882b7028ebfSSpencer Ku                                     const std::string& msg,
1883b7028ebfSSpencer Ku                                     nlohmann::json& logEntryJson)
1884b7028ebfSSpencer Ku {
1885b7028ebfSSpencer Ku     // Fill in the log entry with the gathered data.
1886b7028ebfSSpencer Ku     logEntryJson = {
1887b7028ebfSSpencer Ku         {"@odata.type", "#LogEntry.v1_4_0.LogEntry"},
1888b7028ebfSSpencer Ku         {"@odata.id",
1889b7028ebfSSpencer Ku          "/redfish/v1/Systems/system/LogServices/HostLogger/Entries/" +
1890b7028ebfSSpencer Ku              logEntryID},
1891b7028ebfSSpencer Ku         {"Name", "Host Logger Entry"},
1892b7028ebfSSpencer Ku         {"Id", logEntryID},
1893b7028ebfSSpencer Ku         {"Message", msg},
1894b7028ebfSSpencer Ku         {"EntryType", "Oem"},
1895b7028ebfSSpencer Ku         {"Severity", "OK"},
1896b7028ebfSSpencer Ku         {"OemRecordFormat", "Host Logger Entry"}};
1897b7028ebfSSpencer Ku }
1898b7028ebfSSpencer Ku 
1899b7028ebfSSpencer Ku inline void requestRoutesSystemHostLogger(App& app)
1900b7028ebfSSpencer Ku {
1901b7028ebfSSpencer Ku     BMCWEB_ROUTE(app, "/redfish/v1/Systems/system/LogServices/HostLogger/")
1902b7028ebfSSpencer Ku         .privileges(redfish::privileges::getLogService)
19030fda0f12SGeorge Liu         .methods(
19040fda0f12SGeorge Liu             boost::beast::http::verb::
19050fda0f12SGeorge Liu                 get)([](const crow::Request&,
1906b7028ebfSSpencer Ku                         const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) {
1907b7028ebfSSpencer Ku             asyncResp->res.jsonValue["@odata.id"] =
1908b7028ebfSSpencer Ku                 "/redfish/v1/Systems/system/LogServices/HostLogger";
1909b7028ebfSSpencer Ku             asyncResp->res.jsonValue["@odata.type"] =
1910b7028ebfSSpencer Ku                 "#LogService.v1_1_0.LogService";
1911b7028ebfSSpencer Ku             asyncResp->res.jsonValue["Name"] = "Host Logger Service";
1912b7028ebfSSpencer Ku             asyncResp->res.jsonValue["Description"] = "Host Logger Service";
1913b7028ebfSSpencer Ku             asyncResp->res.jsonValue["Id"] = "HostLogger";
1914b7028ebfSSpencer Ku             asyncResp->res.jsonValue["Entries"] = {
19150fda0f12SGeorge Liu                 {"@odata.id",
19160fda0f12SGeorge Liu                  "/redfish/v1/Systems/system/LogServices/HostLogger/Entries"}};
1917b7028ebfSSpencer Ku         });
1918b7028ebfSSpencer Ku }
1919b7028ebfSSpencer Ku 
1920b7028ebfSSpencer Ku inline void requestRoutesSystemHostLoggerCollection(App& app)
1921b7028ebfSSpencer Ku {
1922b7028ebfSSpencer Ku     BMCWEB_ROUTE(app,
1923b7028ebfSSpencer Ku                  "/redfish/v1/Systems/system/LogServices/HostLogger/Entries/")
1924b7028ebfSSpencer Ku         .privileges(redfish::privileges::getLogEntry)
19250fda0f12SGeorge Liu         .methods(
19260fda0f12SGeorge Liu             boost::beast::http::verb::
19270fda0f12SGeorge Liu                 get)([](const crow::Request& req,
1928b7028ebfSSpencer Ku                         const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) {
1929b7028ebfSSpencer Ku             uint64_t skip = 0;
1930b7028ebfSSpencer Ku             uint64_t top = maxEntriesPerPage; // Show max 1000 entries by
1931b7028ebfSSpencer Ku                                               // default, allow range 1 to
1932b7028ebfSSpencer Ku                                               // 1000 entries per page.
1933b7028ebfSSpencer Ku             if (!getSkipParam(asyncResp, req, skip))
1934b7028ebfSSpencer Ku             {
1935b7028ebfSSpencer Ku                 return;
1936b7028ebfSSpencer Ku             }
1937b7028ebfSSpencer Ku             if (!getTopParam(asyncResp, req, top))
1938b7028ebfSSpencer Ku             {
1939b7028ebfSSpencer Ku                 return;
1940b7028ebfSSpencer Ku             }
1941b7028ebfSSpencer Ku             asyncResp->res.jsonValue["@odata.id"] =
1942b7028ebfSSpencer Ku                 "/redfish/v1/Systems/system/LogServices/HostLogger/Entries";
1943b7028ebfSSpencer Ku             asyncResp->res.jsonValue["@odata.type"] =
1944b7028ebfSSpencer Ku                 "#LogEntryCollection.LogEntryCollection";
1945b7028ebfSSpencer Ku             asyncResp->res.jsonValue["Name"] = "HostLogger Entries";
1946b7028ebfSSpencer Ku             asyncResp->res.jsonValue["Description"] =
1947b7028ebfSSpencer Ku                 "Collection of HostLogger Entries";
19480fda0f12SGeorge Liu             nlohmann::json& logEntryArray = asyncResp->res.jsonValue["Members"];
1949b7028ebfSSpencer Ku             logEntryArray = nlohmann::json::array();
1950b7028ebfSSpencer Ku             asyncResp->res.jsonValue["Members@odata.count"] = 0;
1951b7028ebfSSpencer Ku 
1952b7028ebfSSpencer Ku             std::vector<std::filesystem::path> hostLoggerFiles;
1953b7028ebfSSpencer Ku             if (!getHostLoggerFiles(hostLoggerFolderPath, hostLoggerFiles))
1954b7028ebfSSpencer Ku             {
1955b7028ebfSSpencer Ku                 BMCWEB_LOG_ERROR << "fail to get host log file path";
1956b7028ebfSSpencer Ku                 return;
1957b7028ebfSSpencer Ku             }
1958b7028ebfSSpencer Ku 
1959b7028ebfSSpencer Ku             size_t logCount = 0;
1960b7028ebfSSpencer Ku             // This vector only store the entries we want to expose that
1961b7028ebfSSpencer Ku             // control by skip and top.
1962b7028ebfSSpencer Ku             std::vector<std::string> logEntries;
19630fda0f12SGeorge Liu             if (!getHostLoggerEntries(hostLoggerFiles, skip, top, logEntries,
19640fda0f12SGeorge Liu                                       logCount))
1965b7028ebfSSpencer Ku             {
1966b7028ebfSSpencer Ku                 messages::internalError(asyncResp->res);
1967b7028ebfSSpencer Ku                 return;
1968b7028ebfSSpencer Ku             }
1969b7028ebfSSpencer Ku             // If vector is empty, that means skip value larger than total
1970b7028ebfSSpencer Ku             // log count
1971b7028ebfSSpencer Ku             if (logEntries.size() == 0)
1972b7028ebfSSpencer Ku             {
1973b7028ebfSSpencer Ku                 asyncResp->res.jsonValue["Members@odata.count"] = logCount;
1974b7028ebfSSpencer Ku                 return;
1975b7028ebfSSpencer Ku             }
1976b7028ebfSSpencer Ku             if (logEntries.size() > 0)
1977b7028ebfSSpencer Ku             {
1978b7028ebfSSpencer Ku                 for (size_t i = 0; i < logEntries.size(); i++)
1979b7028ebfSSpencer Ku                 {
1980b7028ebfSSpencer Ku                     logEntryArray.push_back({});
1981b7028ebfSSpencer Ku                     nlohmann::json& hostLogEntry = logEntryArray.back();
1982b7028ebfSSpencer Ku                     fillHostLoggerEntryJson(std::to_string(skip + i),
1983b7028ebfSSpencer Ku                                             logEntries[i], hostLogEntry);
1984b7028ebfSSpencer Ku                 }
1985b7028ebfSSpencer Ku 
1986b7028ebfSSpencer Ku                 asyncResp->res.jsonValue["Members@odata.count"] = logCount;
1987b7028ebfSSpencer Ku                 if (skip + top < logCount)
1988b7028ebfSSpencer Ku                 {
1989b7028ebfSSpencer Ku                     asyncResp->res.jsonValue["Members@odata.nextLink"] =
19900fda0f12SGeorge Liu                         "/redfish/v1/Systems/system/LogServices/HostLogger/Entries?$skip=" +
1991b7028ebfSSpencer Ku                         std::to_string(skip + top);
1992b7028ebfSSpencer Ku                 }
1993b7028ebfSSpencer Ku             }
1994b7028ebfSSpencer Ku         });
1995b7028ebfSSpencer Ku }
1996b7028ebfSSpencer Ku 
1997b7028ebfSSpencer Ku inline void requestRoutesSystemHostLoggerLogEntry(App& app)
1998b7028ebfSSpencer Ku {
1999b7028ebfSSpencer Ku     BMCWEB_ROUTE(
2000b7028ebfSSpencer Ku         app, "/redfish/v1/Systems/system/LogServices/HostLogger/Entries/<str>/")
2001b7028ebfSSpencer Ku         .privileges(redfish::privileges::getLogEntry)
2002b7028ebfSSpencer Ku         .methods(boost::beast::http::verb::get)(
2003b7028ebfSSpencer Ku             [](const crow::Request&,
2004b7028ebfSSpencer Ku                const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
2005b7028ebfSSpencer Ku                const std::string& param) {
2006b7028ebfSSpencer Ku                 const std::string& targetID = param;
2007b7028ebfSSpencer Ku 
2008b7028ebfSSpencer Ku                 uint64_t idInt = 0;
2009b7028ebfSSpencer Ku                 auto [ptr, ec] = std::from_chars(
2010b7028ebfSSpencer Ku                     targetID.data(), targetID.data() + targetID.size(), idInt);
2011b7028ebfSSpencer Ku                 if (ec == std::errc::invalid_argument)
2012b7028ebfSSpencer Ku                 {
2013b7028ebfSSpencer Ku                     messages::resourceMissingAtURI(asyncResp->res, targetID);
2014b7028ebfSSpencer Ku                     return;
2015b7028ebfSSpencer Ku                 }
2016b7028ebfSSpencer Ku                 if (ec == std::errc::result_out_of_range)
2017b7028ebfSSpencer Ku                 {
2018b7028ebfSSpencer Ku                     messages::resourceMissingAtURI(asyncResp->res, targetID);
2019b7028ebfSSpencer Ku                     return;
2020b7028ebfSSpencer Ku                 }
2021b7028ebfSSpencer Ku 
2022b7028ebfSSpencer Ku                 std::vector<std::filesystem::path> hostLoggerFiles;
2023b7028ebfSSpencer Ku                 if (!getHostLoggerFiles(hostLoggerFolderPath, hostLoggerFiles))
2024b7028ebfSSpencer Ku                 {
2025b7028ebfSSpencer Ku                     BMCWEB_LOG_ERROR << "fail to get host log file path";
2026b7028ebfSSpencer Ku                     return;
2027b7028ebfSSpencer Ku                 }
2028b7028ebfSSpencer Ku 
2029b7028ebfSSpencer Ku                 size_t logCount = 0;
2030b7028ebfSSpencer Ku                 uint64_t top = 1;
2031b7028ebfSSpencer Ku                 std::vector<std::string> logEntries;
2032b7028ebfSSpencer Ku                 // We can get specific entry by skip and top. For example, if we
2033b7028ebfSSpencer Ku                 // want to get nth entry, we can set skip = n-1 and top = 1 to
2034b7028ebfSSpencer Ku                 // get that entry
2035b7028ebfSSpencer Ku                 if (!getHostLoggerEntries(hostLoggerFiles, idInt, top,
2036b7028ebfSSpencer Ku                                           logEntries, logCount))
2037b7028ebfSSpencer Ku                 {
2038b7028ebfSSpencer Ku                     messages::internalError(asyncResp->res);
2039b7028ebfSSpencer Ku                     return;
2040b7028ebfSSpencer Ku                 }
2041b7028ebfSSpencer Ku 
2042b7028ebfSSpencer Ku                 if (!logEntries.empty())
2043b7028ebfSSpencer Ku                 {
2044b7028ebfSSpencer Ku                     fillHostLoggerEntryJson(targetID, logEntries[0],
2045b7028ebfSSpencer Ku                                             asyncResp->res.jsonValue);
2046b7028ebfSSpencer Ku                     return;
2047b7028ebfSSpencer Ku                 }
2048b7028ebfSSpencer Ku 
2049b7028ebfSSpencer Ku                 // Requested ID was not found
2050b7028ebfSSpencer Ku                 messages::resourceMissingAtURI(asyncResp->res, targetID);
2051b7028ebfSSpencer Ku             });
2052b7028ebfSSpencer Ku }
2053b7028ebfSSpencer Ku 
20547e860f15SJohn Edward Broadbent inline void requestRoutesBMCLogServiceCollection(App& app)
20551da66f75SEd Tanous {
20567e860f15SJohn Edward Broadbent     BMCWEB_ROUTE(app, "/redfish/v1/Managers/bmc/LogServices/")
2057ad89dcf0SGunnar Mills         .privileges(redfish::privileges::getLogServiceCollection)
20587e860f15SJohn Edward Broadbent         .methods(boost::beast::http::verb::get)(
20597e860f15SJohn Edward Broadbent             [](const crow::Request&,
20607e860f15SJohn Edward Broadbent                const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) {
20617e860f15SJohn Edward Broadbent                 // Collections don't include the static data added by SubRoute
20627e860f15SJohn Edward Broadbent                 // because it has a duplicate entry for members
2063e1f26343SJason M. Bills                 asyncResp->res.jsonValue["@odata.type"] =
20641da66f75SEd Tanous                     "#LogServiceCollection.LogServiceCollection";
2065e1f26343SJason M. Bills                 asyncResp->res.jsonValue["@odata.id"] =
2066e1f26343SJason M. Bills                     "/redfish/v1/Managers/bmc/LogServices";
20677e860f15SJohn Edward Broadbent                 asyncResp->res.jsonValue["Name"] =
20687e860f15SJohn Edward Broadbent                     "Open BMC Log Services Collection";
2069e1f26343SJason M. Bills                 asyncResp->res.jsonValue["Description"] =
20701da66f75SEd Tanous                     "Collection of LogServices for this Manager";
20717e860f15SJohn Edward Broadbent                 nlohmann::json& logServiceArray =
20727e860f15SJohn Edward Broadbent                     asyncResp->res.jsonValue["Members"];
2073c4bf6374SJason M. Bills                 logServiceArray = nlohmann::json::array();
20745cb1dd27SAsmitha Karunanithi #ifdef BMCWEB_ENABLE_REDFISH_DUMP_LOG
20755cb1dd27SAsmitha Karunanithi                 logServiceArray.push_back(
20767e860f15SJohn Edward Broadbent                     {{"@odata.id",
20777e860f15SJohn Edward Broadbent                       "/redfish/v1/Managers/bmc/LogServices/Dump"}});
20785cb1dd27SAsmitha Karunanithi #endif
2079c4bf6374SJason M. Bills #ifdef BMCWEB_ENABLE_REDFISH_BMC_JOURNAL
2080c4bf6374SJason M. Bills                 logServiceArray.push_back(
20817e860f15SJohn Edward Broadbent                     {{"@odata.id",
20827e860f15SJohn Edward Broadbent                       "/redfish/v1/Managers/bmc/LogServices/Journal"}});
2083c4bf6374SJason M. Bills #endif
2084e1f26343SJason M. Bills                 asyncResp->res.jsonValue["Members@odata.count"] =
2085c4bf6374SJason M. Bills                     logServiceArray.size();
20867e860f15SJohn Edward Broadbent             });
2087e1f26343SJason M. Bills }
2088e1f26343SJason M. Bills 
20897e860f15SJohn Edward Broadbent inline void requestRoutesBMCJournalLogService(App& app)
2090e1f26343SJason M. Bills {
20917e860f15SJohn Edward Broadbent     BMCWEB_ROUTE(app, "/redfish/v1/Managers/bmc/LogServices/Journal/")
2092ed398213SEd Tanous         .privileges(redfish::privileges::getLogService)
20937e860f15SJohn Edward Broadbent         .methods(boost::beast::http::verb::get)(
20947e860f15SJohn Edward Broadbent             [](const crow::Request&,
20957e860f15SJohn Edward Broadbent                const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
20968d1b46d7Szhanghch05 
20977e860f15SJohn Edward Broadbent             {
2098e1f26343SJason M. Bills                 asyncResp->res.jsonValue["@odata.type"] =
2099e1f26343SJason M. Bills                     "#LogService.v1_1_0.LogService";
21000f74e643SEd Tanous                 asyncResp->res.jsonValue["@odata.id"] =
21010f74e643SEd Tanous                     "/redfish/v1/Managers/bmc/LogServices/Journal";
21027e860f15SJohn Edward Broadbent                 asyncResp->res.jsonValue["Name"] =
21037e860f15SJohn Edward Broadbent                     "Open BMC Journal Log Service";
21047e860f15SJohn Edward Broadbent                 asyncResp->res.jsonValue["Description"] =
21057e860f15SJohn Edward Broadbent                     "BMC Journal Log Service";
2106c4bf6374SJason M. Bills                 asyncResp->res.jsonValue["Id"] = "BMC Journal";
2107e1f26343SJason M. Bills                 asyncResp->res.jsonValue["OverWritePolicy"] = "WrapsWhenFull";
21087c8c4058STejas Patil 
21097c8c4058STejas Patil                 std::pair<std::string, std::string> redfishDateTimeOffset =
21107c8c4058STejas Patil                     crow::utility::getDateTimeOffsetNow();
21117c8c4058STejas Patil                 asyncResp->res.jsonValue["DateTime"] =
21127c8c4058STejas Patil                     redfishDateTimeOffset.first;
21137c8c4058STejas Patil                 asyncResp->res.jsonValue["DateTimeLocalOffset"] =
21147c8c4058STejas Patil                     redfishDateTimeOffset.second;
21157c8c4058STejas Patil 
2116cd50aa42SJason M. Bills                 asyncResp->res.jsonValue["Entries"] = {
2117cd50aa42SJason M. Bills                     {"@odata.id",
2118086be238SEd Tanous                      "/redfish/v1/Managers/bmc/LogServices/Journal/Entries"}};
21197e860f15SJohn Edward Broadbent             });
2120e1f26343SJason M. Bills }
2121e1f26343SJason M. Bills 
2122c4bf6374SJason M. Bills static int fillBMCJournalLogEntryJson(const std::string& bmcJournalLogEntryID,
2123e1f26343SJason M. Bills                                       sd_journal* journal,
2124c4bf6374SJason M. Bills                                       nlohmann::json& bmcJournalLogEntryJson)
2125e1f26343SJason M. Bills {
2126e1f26343SJason M. Bills     // Get the Log Entry contents
2127e1f26343SJason M. Bills     int ret = 0;
2128e1f26343SJason M. Bills 
2129a8fe54f0SJason M. Bills     std::string message;
2130a8fe54f0SJason M. Bills     std::string_view syslogID;
2131a8fe54f0SJason M. Bills     ret = getJournalMetadata(journal, "SYSLOG_IDENTIFIER", syslogID);
2132a8fe54f0SJason M. Bills     if (ret < 0)
2133a8fe54f0SJason M. Bills     {
2134a8fe54f0SJason M. Bills         BMCWEB_LOG_ERROR << "Failed to read SYSLOG_IDENTIFIER field: "
2135a8fe54f0SJason M. Bills                          << strerror(-ret);
2136a8fe54f0SJason M. Bills     }
2137a8fe54f0SJason M. Bills     if (!syslogID.empty())
2138a8fe54f0SJason M. Bills     {
2139a8fe54f0SJason M. Bills         message += std::string(syslogID) + ": ";
2140a8fe54f0SJason M. Bills     }
2141a8fe54f0SJason M. Bills 
214239e77504SEd Tanous     std::string_view msg;
214316428a1aSJason M. Bills     ret = getJournalMetadata(journal, "MESSAGE", msg);
2144e1f26343SJason M. Bills     if (ret < 0)
2145e1f26343SJason M. Bills     {
2146e1f26343SJason M. Bills         BMCWEB_LOG_ERROR << "Failed to read MESSAGE field: " << strerror(-ret);
2147e1f26343SJason M. Bills         return 1;
2148e1f26343SJason M. Bills     }
2149a8fe54f0SJason M. Bills     message += std::string(msg);
2150e1f26343SJason M. Bills 
2151e1f26343SJason M. Bills     // Get the severity from the PRIORITY field
2152271584abSEd Tanous     long int severity = 8; // Default to an invalid priority
215316428a1aSJason M. Bills     ret = getJournalMetadata(journal, "PRIORITY", 10, severity);
2154e1f26343SJason M. Bills     if (ret < 0)
2155e1f26343SJason M. Bills     {
2156e1f26343SJason M. Bills         BMCWEB_LOG_ERROR << "Failed to read PRIORITY field: " << strerror(-ret);
2157e1f26343SJason M. Bills     }
2158e1f26343SJason M. Bills 
2159e1f26343SJason M. Bills     // Get the Created time from the timestamp
216016428a1aSJason M. Bills     std::string entryTimeStr;
216116428a1aSJason M. Bills     if (!getEntryTimestamp(journal, entryTimeStr))
2162e1f26343SJason M. Bills     {
216316428a1aSJason M. Bills         return 1;
2164e1f26343SJason M. Bills     }
2165e1f26343SJason M. Bills 
2166e1f26343SJason M. Bills     // Fill in the log entry with the gathered data
2167c4bf6374SJason M. Bills     bmcJournalLogEntryJson = {
2168647b3cdcSGeorge Liu         {"@odata.type", "#LogEntry.v1_8_0.LogEntry"},
2169c4bf6374SJason M. Bills         {"@odata.id", "/redfish/v1/Managers/bmc/LogServices/Journal/Entries/" +
2170c4bf6374SJason M. Bills                           bmcJournalLogEntryID},
2171e1f26343SJason M. Bills         {"Name", "BMC Journal Entry"},
2172c4bf6374SJason M. Bills         {"Id", bmcJournalLogEntryID},
2173a8fe54f0SJason M. Bills         {"Message", std::move(message)},
2174e1f26343SJason M. Bills         {"EntryType", "Oem"},
2175738c1e61SPatrick Williams         {"Severity", severity <= 2   ? "Critical"
2176738c1e61SPatrick Williams                      : severity <= 4 ? "Warning"
2177738c1e61SPatrick Williams                                      : "OK"},
2178086be238SEd Tanous         {"OemRecordFormat", "BMC Journal Entry"},
2179e1f26343SJason M. Bills         {"Created", std::move(entryTimeStr)}};
2180e1f26343SJason M. Bills     return 0;
2181e1f26343SJason M. Bills }
2182e1f26343SJason M. Bills 
21837e860f15SJohn Edward Broadbent inline void requestRoutesBMCJournalLogEntryCollection(App& app)
2184e1f26343SJason M. Bills {
21857e860f15SJohn Edward Broadbent     BMCWEB_ROUTE(app, "/redfish/v1/Managers/bmc/LogServices/Journal/Entries/")
2186ed398213SEd Tanous         .privileges(redfish::privileges::getLogEntryCollection)
21870fda0f12SGeorge Liu         .methods(
21880fda0f12SGeorge Liu             boost::beast::http::verb::
21890fda0f12SGeorge Liu                 get)([](const crow::Request& req,
21907e860f15SJohn Edward Broadbent                         const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) {
2191193ad2faSJason M. Bills             static constexpr const long maxEntriesPerPage = 1000;
2192271584abSEd Tanous             uint64_t skip = 0;
2193271584abSEd Tanous             uint64_t top = maxEntriesPerPage; // Show max entries by default
21948d1b46d7Szhanghch05             if (!getSkipParam(asyncResp, req, skip))
2195193ad2faSJason M. Bills             {
2196193ad2faSJason M. Bills                 return;
2197193ad2faSJason M. Bills             }
21988d1b46d7Szhanghch05             if (!getTopParam(asyncResp, req, top))
2199193ad2faSJason M. Bills             {
2200193ad2faSJason M. Bills                 return;
2201193ad2faSJason M. Bills             }
22027e860f15SJohn Edward Broadbent             // Collections don't include the static data added by SubRoute
22037e860f15SJohn Edward Broadbent             // because it has a duplicate entry for members
2204e1f26343SJason M. Bills             asyncResp->res.jsonValue["@odata.type"] =
2205e1f26343SJason M. Bills                 "#LogEntryCollection.LogEntryCollection";
22060f74e643SEd Tanous             asyncResp->res.jsonValue["@odata.id"] =
22070f74e643SEd Tanous                 "/redfish/v1/Managers/bmc/LogServices/Journal/Entries";
2208e1f26343SJason M. Bills             asyncResp->res.jsonValue["Name"] = "Open BMC Journal Entries";
2209e1f26343SJason M. Bills             asyncResp->res.jsonValue["Description"] =
2210e1f26343SJason M. Bills                 "Collection of BMC Journal Entries";
22110fda0f12SGeorge Liu             nlohmann::json& logEntryArray = asyncResp->res.jsonValue["Members"];
2212e1f26343SJason M. Bills             logEntryArray = nlohmann::json::array();
2213e1f26343SJason M. Bills 
22147e860f15SJohn Edward Broadbent             // Go through the journal and use the timestamp to create a
22157e860f15SJohn Edward Broadbent             // unique ID for each entry
2216e1f26343SJason M. Bills             sd_journal* journalTmp = nullptr;
2217e1f26343SJason M. Bills             int ret = sd_journal_open(&journalTmp, SD_JOURNAL_LOCAL_ONLY);
2218e1f26343SJason M. Bills             if (ret < 0)
2219e1f26343SJason M. Bills             {
22207e860f15SJohn Edward Broadbent                 BMCWEB_LOG_ERROR << "failed to open journal: "
22217e860f15SJohn Edward Broadbent                                  << strerror(-ret);
2222f12894f8SJason M. Bills                 messages::internalError(asyncResp->res);
2223e1f26343SJason M. Bills                 return;
2224e1f26343SJason M. Bills             }
22250fda0f12SGeorge Liu             std::unique_ptr<sd_journal, decltype(&sd_journal_close)> journal(
22260fda0f12SGeorge Liu                 journalTmp, sd_journal_close);
2227e1f26343SJason M. Bills             journalTmp = nullptr;
2228b01bf299SEd Tanous             uint64_t entryCount = 0;
2229e85d6b16SJason M. Bills             // Reset the unique ID on the first entry
2230e85d6b16SJason M. Bills             bool firstEntry = true;
2231e1f26343SJason M. Bills             SD_JOURNAL_FOREACH(journal.get())
2232e1f26343SJason M. Bills             {
2233193ad2faSJason M. Bills                 entryCount++;
22347e860f15SJohn Edward Broadbent                 // Handle paging using skip (number of entries to skip from
22357e860f15SJohn Edward Broadbent                 // the start) and top (number of entries to display)
2236193ad2faSJason M. Bills                 if (entryCount <= skip || entryCount > skip + top)
2237193ad2faSJason M. Bills                 {
2238193ad2faSJason M. Bills                     continue;
2239193ad2faSJason M. Bills                 }
2240193ad2faSJason M. Bills 
224116428a1aSJason M. Bills                 std::string idStr;
2242e85d6b16SJason M. Bills                 if (!getUniqueEntryID(journal.get(), idStr, firstEntry))
2243e1f26343SJason M. Bills                 {
2244e1f26343SJason M. Bills                     continue;
2245e1f26343SJason M. Bills                 }
2246e1f26343SJason M. Bills 
2247e85d6b16SJason M. Bills                 if (firstEntry)
2248e85d6b16SJason M. Bills                 {
2249e85d6b16SJason M. Bills                     firstEntry = false;
2250e85d6b16SJason M. Bills                 }
2251e85d6b16SJason M. Bills 
2252e1f26343SJason M. Bills                 logEntryArray.push_back({});
2253c4bf6374SJason M. Bills                 nlohmann::json& bmcJournalLogEntry = logEntryArray.back();
2254c4bf6374SJason M. Bills                 if (fillBMCJournalLogEntryJson(idStr, journal.get(),
2255c4bf6374SJason M. Bills                                                bmcJournalLogEntry) != 0)
2256e1f26343SJason M. Bills                 {
2257f12894f8SJason M. Bills                     messages::internalError(asyncResp->res);
2258e1f26343SJason M. Bills                     return;
2259e1f26343SJason M. Bills                 }
2260e1f26343SJason M. Bills             }
2261193ad2faSJason M. Bills             asyncResp->res.jsonValue["Members@odata.count"] = entryCount;
2262193ad2faSJason M. Bills             if (skip + top < entryCount)
2263193ad2faSJason M. Bills             {
2264193ad2faSJason M. Bills                 asyncResp->res.jsonValue["Members@odata.nextLink"] =
22650fda0f12SGeorge Liu                     "/redfish/v1/Managers/bmc/LogServices/Journal/Entries?$skip=" +
2266193ad2faSJason M. Bills                     std::to_string(skip + top);
2267193ad2faSJason M. Bills             }
22687e860f15SJohn Edward Broadbent         });
2269e1f26343SJason M. Bills }
2270e1f26343SJason M. Bills 
22717e860f15SJohn Edward Broadbent inline void requestRoutesBMCJournalLogEntry(App& app)
2272e1f26343SJason M. Bills {
22737e860f15SJohn Edward Broadbent     BMCWEB_ROUTE(app,
22747e860f15SJohn Edward Broadbent                  "/redfish/v1/Managers/bmc/LogServices/Journal/Entries/<str>/")
2275ed398213SEd Tanous         .privileges(redfish::privileges::getLogEntry)
22767e860f15SJohn Edward Broadbent         .methods(boost::beast::http::verb::get)(
22777e860f15SJohn Edward Broadbent             [](const crow::Request&,
22787e860f15SJohn Edward Broadbent                const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
22797e860f15SJohn Edward Broadbent                const std::string& entryID) {
2280e1f26343SJason M. Bills                 // Convert the unique ID back to a timestamp to find the entry
2281e1f26343SJason M. Bills                 uint64_t ts = 0;
2282271584abSEd Tanous                 uint64_t index = 0;
22838d1b46d7Szhanghch05                 if (!getTimestampFromID(asyncResp, entryID, ts, index))
2284e1f26343SJason M. Bills                 {
228516428a1aSJason M. Bills                     return;
2286e1f26343SJason M. Bills                 }
2287e1f26343SJason M. Bills 
2288e1f26343SJason M. Bills                 sd_journal* journalTmp = nullptr;
2289e1f26343SJason M. Bills                 int ret = sd_journal_open(&journalTmp, SD_JOURNAL_LOCAL_ONLY);
2290e1f26343SJason M. Bills                 if (ret < 0)
2291e1f26343SJason M. Bills                 {
22927e860f15SJohn Edward Broadbent                     BMCWEB_LOG_ERROR << "failed to open journal: "
22937e860f15SJohn Edward Broadbent                                      << strerror(-ret);
2294f12894f8SJason M. Bills                     messages::internalError(asyncResp->res);
2295e1f26343SJason M. Bills                     return;
2296e1f26343SJason M. Bills                 }
22977e860f15SJohn Edward Broadbent                 std::unique_ptr<sd_journal, decltype(&sd_journal_close)>
22987e860f15SJohn Edward Broadbent                     journal(journalTmp, sd_journal_close);
2299e1f26343SJason M. Bills                 journalTmp = nullptr;
23007e860f15SJohn Edward Broadbent                 // Go to the timestamp in the log and move to the entry at the
23017e860f15SJohn Edward Broadbent                 // index tracking the unique ID
2302af07e3f5SJason M. Bills                 std::string idStr;
2303af07e3f5SJason M. Bills                 bool firstEntry = true;
2304e1f26343SJason M. Bills                 ret = sd_journal_seek_realtime_usec(journal.get(), ts);
23052056b6d1SManojkiran Eda                 if (ret < 0)
23062056b6d1SManojkiran Eda                 {
23072056b6d1SManojkiran Eda                     BMCWEB_LOG_ERROR << "failed to seek to an entry in journal"
23082056b6d1SManojkiran Eda                                      << strerror(-ret);
23092056b6d1SManojkiran Eda                     messages::internalError(asyncResp->res);
23102056b6d1SManojkiran Eda                     return;
23112056b6d1SManojkiran Eda                 }
2312271584abSEd Tanous                 for (uint64_t i = 0; i <= index; i++)
2313e1f26343SJason M. Bills                 {
2314e1f26343SJason M. Bills                     sd_journal_next(journal.get());
2315af07e3f5SJason M. Bills                     if (!getUniqueEntryID(journal.get(), idStr, firstEntry))
2316af07e3f5SJason M. Bills                     {
2317af07e3f5SJason M. Bills                         messages::internalError(asyncResp->res);
2318af07e3f5SJason M. Bills                         return;
2319af07e3f5SJason M. Bills                     }
2320af07e3f5SJason M. Bills                     if (firstEntry)
2321af07e3f5SJason M. Bills                     {
2322af07e3f5SJason M. Bills                         firstEntry = false;
2323af07e3f5SJason M. Bills                     }
2324e1f26343SJason M. Bills                 }
2325c4bf6374SJason M. Bills                 // Confirm that the entry ID matches what was requested
2326af07e3f5SJason M. Bills                 if (idStr != entryID)
2327c4bf6374SJason M. Bills                 {
2328c4bf6374SJason M. Bills                     messages::resourceMissingAtURI(asyncResp->res, entryID);
2329c4bf6374SJason M. Bills                     return;
2330c4bf6374SJason M. Bills                 }
2331c4bf6374SJason M. Bills 
2332c4bf6374SJason M. Bills                 if (fillBMCJournalLogEntryJson(entryID, journal.get(),
2333e1f26343SJason M. Bills                                                asyncResp->res.jsonValue) != 0)
2334e1f26343SJason M. Bills                 {
2335f12894f8SJason M. Bills                     messages::internalError(asyncResp->res);
2336e1f26343SJason M. Bills                     return;
2337e1f26343SJason M. Bills                 }
23387e860f15SJohn Edward Broadbent             });
2339c9bb6861Sraviteja-b }
2340c9bb6861Sraviteja-b 
23417e860f15SJohn Edward Broadbent inline void requestRoutesBMCDumpService(App& app)
2342c9bb6861Sraviteja-b {
23437e860f15SJohn Edward Broadbent     BMCWEB_ROUTE(app, "/redfish/v1/Managers/bmc/LogServices/Dump/")
2344ed398213SEd Tanous         .privileges(redfish::privileges::getLogService)
23450fda0f12SGeorge Liu         .methods(
23460fda0f12SGeorge Liu             boost::beast::http::verb::
23470fda0f12SGeorge Liu                 get)([](const crow::Request&,
23487e860f15SJohn Edward Broadbent                         const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) {
2349c9bb6861Sraviteja-b             asyncResp->res.jsonValue["@odata.id"] =
23505cb1dd27SAsmitha Karunanithi                 "/redfish/v1/Managers/bmc/LogServices/Dump";
2351c9bb6861Sraviteja-b             asyncResp->res.jsonValue["@odata.type"] =
2352d337bb72SAsmitha Karunanithi                 "#LogService.v1_2_0.LogService";
2353c9bb6861Sraviteja-b             asyncResp->res.jsonValue["Name"] = "Dump LogService";
23545cb1dd27SAsmitha Karunanithi             asyncResp->res.jsonValue["Description"] = "BMC Dump LogService";
23555cb1dd27SAsmitha Karunanithi             asyncResp->res.jsonValue["Id"] = "Dump";
2356c9bb6861Sraviteja-b             asyncResp->res.jsonValue["OverWritePolicy"] = "WrapsWhenFull";
23577c8c4058STejas Patil 
23587c8c4058STejas Patil             std::pair<std::string, std::string> redfishDateTimeOffset =
23597c8c4058STejas Patil                 crow::utility::getDateTimeOffsetNow();
23600fda0f12SGeorge Liu             asyncResp->res.jsonValue["DateTime"] = redfishDateTimeOffset.first;
23617c8c4058STejas Patil             asyncResp->res.jsonValue["DateTimeLocalOffset"] =
23627c8c4058STejas Patil                 redfishDateTimeOffset.second;
23637c8c4058STejas Patil 
2364c9bb6861Sraviteja-b             asyncResp->res.jsonValue["Entries"] = {
23657e860f15SJohn Edward Broadbent                 {"@odata.id",
23667e860f15SJohn Edward Broadbent                  "/redfish/v1/Managers/bmc/LogServices/Dump/Entries"}};
23675cb1dd27SAsmitha Karunanithi             asyncResp->res.jsonValue["Actions"] = {
23685cb1dd27SAsmitha Karunanithi                 {"#LogService.ClearLog",
23690fda0f12SGeorge Liu                  {{"target",
23700fda0f12SGeorge Liu                    "/redfish/v1/Managers/bmc/LogServices/Dump/Actions/LogService.ClearLog"}}},
2371d337bb72SAsmitha Karunanithi                 {"#LogService.CollectDiagnosticData",
23720fda0f12SGeorge Liu                  {{"target",
23730fda0f12SGeorge Liu                    "/redfish/v1/Managers/bmc/LogServices/Dump/Actions/LogService.CollectDiagnosticData"}}}};
23747e860f15SJohn Edward Broadbent         });
2375c9bb6861Sraviteja-b }
2376c9bb6861Sraviteja-b 
23777e860f15SJohn Edward Broadbent inline void requestRoutesBMCDumpEntryCollection(App& app)
23787e860f15SJohn Edward Broadbent {
23797e860f15SJohn Edward Broadbent 
2380c9bb6861Sraviteja-b     /**
2381c9bb6861Sraviteja-b      * Functions triggers appropriate requests on DBus
2382c9bb6861Sraviteja-b      */
23837e860f15SJohn Edward Broadbent     BMCWEB_ROUTE(app, "/redfish/v1/Managers/bmc/LogServices/Dump/Entries/")
2384ed398213SEd Tanous         .privileges(redfish::privileges::getLogEntryCollection)
23857e860f15SJohn Edward Broadbent         .methods(boost::beast::http::verb::get)(
23867e860f15SJohn Edward Broadbent             [](const crow::Request&,
23877e860f15SJohn Edward Broadbent                const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) {
2388c9bb6861Sraviteja-b                 asyncResp->res.jsonValue["@odata.type"] =
2389c9bb6861Sraviteja-b                     "#LogEntryCollection.LogEntryCollection";
2390c9bb6861Sraviteja-b                 asyncResp->res.jsonValue["@odata.id"] =
23915cb1dd27SAsmitha Karunanithi                     "/redfish/v1/Managers/bmc/LogServices/Dump/Entries";
23925cb1dd27SAsmitha Karunanithi                 asyncResp->res.jsonValue["Name"] = "BMC Dump Entries";
2393c9bb6861Sraviteja-b                 asyncResp->res.jsonValue["Description"] =
23945cb1dd27SAsmitha Karunanithi                     "Collection of BMC Dump Entries";
2395c9bb6861Sraviteja-b 
23965cb1dd27SAsmitha Karunanithi                 getDumpEntryCollection(asyncResp, "BMC");
23977e860f15SJohn Edward Broadbent             });
2398c9bb6861Sraviteja-b }
2399c9bb6861Sraviteja-b 
24007e860f15SJohn Edward Broadbent inline void requestRoutesBMCDumpEntry(App& app)
2401c9bb6861Sraviteja-b {
24027e860f15SJohn Edward Broadbent     BMCWEB_ROUTE(app,
24037e860f15SJohn Edward Broadbent                  "/redfish/v1/Managers/bmc/LogServices/Dump/Entries/<str>/")
2404ed398213SEd Tanous         .privileges(redfish::privileges::getLogEntry)
24057e860f15SJohn Edward Broadbent         .methods(boost::beast::http::verb::get)(
24067e860f15SJohn Edward Broadbent             [](const crow::Request&,
24077e860f15SJohn Edward Broadbent                const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
24087e860f15SJohn Edward Broadbent                const std::string& param) {
24097e860f15SJohn Edward Broadbent                 getDumpEntryById(asyncResp, param, "BMC");
24107e860f15SJohn Edward Broadbent             });
24117e860f15SJohn Edward Broadbent     BMCWEB_ROUTE(app,
24127e860f15SJohn Edward Broadbent                  "/redfish/v1/Managers/bmc/LogServices/Dump/Entries/<str>/")
2413ed398213SEd Tanous         .privileges(redfish::privileges::deleteLogEntry)
24147e860f15SJohn Edward Broadbent         .methods(boost::beast::http::verb::delete_)(
24157e860f15SJohn Edward Broadbent             [](const crow::Request&,
24167e860f15SJohn Edward Broadbent                const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
24177e860f15SJohn Edward Broadbent                const std::string& param) {
24187e860f15SJohn Edward Broadbent                 deleteDumpEntry(asyncResp, param, "bmc");
24197e860f15SJohn Edward Broadbent             });
2420c9bb6861Sraviteja-b }
2421c9bb6861Sraviteja-b 
24227e860f15SJohn Edward Broadbent inline void requestRoutesBMCDumpCreate(App& app)
2423c9bb6861Sraviteja-b {
24248d1b46d7Szhanghch05 
24250fda0f12SGeorge Liu     BMCWEB_ROUTE(
24260fda0f12SGeorge Liu         app,
24270fda0f12SGeorge Liu         "/redfish/v1/Managers/bmc/LogServices/Dump/Actions/LogService.CollectDiagnosticData/")
2428ed398213SEd Tanous         .privileges(redfish::privileges::postLogService)
24297e860f15SJohn Edward Broadbent         .methods(boost::beast::http::verb::post)(
24307e860f15SJohn Edward Broadbent             [](const crow::Request& req,
24317e860f15SJohn Edward Broadbent                const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) {
24328d1b46d7Szhanghch05                 createDump(asyncResp, req, "BMC");
24337e860f15SJohn Edward Broadbent             });
2434a43be80fSAsmitha Karunanithi }
2435a43be80fSAsmitha Karunanithi 
24367e860f15SJohn Edward Broadbent inline void requestRoutesBMCDumpClear(App& app)
243780319af1SAsmitha Karunanithi {
24380fda0f12SGeorge Liu     BMCWEB_ROUTE(
24390fda0f12SGeorge Liu         app,
24400fda0f12SGeorge Liu         "/redfish/v1/Managers/bmc/LogServices/Dump/Actions/LogService.ClearLog/")
2441ed398213SEd Tanous         .privileges(redfish::privileges::postLogService)
24427e860f15SJohn Edward Broadbent         .methods(boost::beast::http::verb::post)(
24437e860f15SJohn Edward Broadbent             [](const crow::Request&,
24447e860f15SJohn Edward Broadbent                const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) {
24458d1b46d7Szhanghch05                 clearDump(asyncResp, "BMC");
24467e860f15SJohn Edward Broadbent             });
24475cb1dd27SAsmitha Karunanithi }
24485cb1dd27SAsmitha Karunanithi 
24497e860f15SJohn Edward Broadbent inline void requestRoutesSystemDumpService(App& app)
24505cb1dd27SAsmitha Karunanithi {
24517e860f15SJohn Edward Broadbent     BMCWEB_ROUTE(app, "/redfish/v1/Systems/system/LogServices/Dump/")
2452ed398213SEd Tanous         .privileges(redfish::privileges::getLogService)
24537e860f15SJohn Edward Broadbent         .methods(boost::beast::http::verb::get)(
24547e860f15SJohn Edward Broadbent             [](const crow::Request&,
24557e860f15SJohn Edward Broadbent                const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
24565cb1dd27SAsmitha Karunanithi 
24577e860f15SJohn Edward Broadbent             {
24585cb1dd27SAsmitha Karunanithi                 asyncResp->res.jsonValue["@odata.id"] =
24595cb1dd27SAsmitha Karunanithi                     "/redfish/v1/Systems/system/LogServices/Dump";
24605cb1dd27SAsmitha Karunanithi                 asyncResp->res.jsonValue["@odata.type"] =
2461d337bb72SAsmitha Karunanithi                     "#LogService.v1_2_0.LogService";
24625cb1dd27SAsmitha Karunanithi                 asyncResp->res.jsonValue["Name"] = "Dump LogService";
24637e860f15SJohn Edward Broadbent                 asyncResp->res.jsonValue["Description"] =
24647e860f15SJohn Edward Broadbent                     "System Dump LogService";
24655cb1dd27SAsmitha Karunanithi                 asyncResp->res.jsonValue["Id"] = "Dump";
24665cb1dd27SAsmitha Karunanithi                 asyncResp->res.jsonValue["OverWritePolicy"] = "WrapsWhenFull";
24677c8c4058STejas Patil 
24687c8c4058STejas Patil                 std::pair<std::string, std::string> redfishDateTimeOffset =
24697c8c4058STejas Patil                     crow::utility::getDateTimeOffsetNow();
24707c8c4058STejas Patil                 asyncResp->res.jsonValue["DateTime"] =
24717c8c4058STejas Patil                     redfishDateTimeOffset.first;
24727c8c4058STejas Patil                 asyncResp->res.jsonValue["DateTimeLocalOffset"] =
24737c8c4058STejas Patil                     redfishDateTimeOffset.second;
24747c8c4058STejas Patil 
24755cb1dd27SAsmitha Karunanithi                 asyncResp->res.jsonValue["Entries"] = {
24765cb1dd27SAsmitha Karunanithi                     {"@odata.id",
24775cb1dd27SAsmitha Karunanithi                      "/redfish/v1/Systems/system/LogServices/Dump/Entries"}};
24785cb1dd27SAsmitha Karunanithi                 asyncResp->res.jsonValue["Actions"] = {
24795cb1dd27SAsmitha Karunanithi                     {"#LogService.ClearLog",
24807e860f15SJohn Edward Broadbent                      {{"target",
24810fda0f12SGeorge Liu                        "/redfish/v1/Systems/system/LogServices/Dump/Actions/LogService.ClearLog"}}},
2482d337bb72SAsmitha Karunanithi                     {"#LogService.CollectDiagnosticData",
24837e860f15SJohn Edward Broadbent                      {{"target",
24840fda0f12SGeorge Liu                        "/redfish/v1/Systems/system/LogServices/Dump/Actions/LogService.CollectDiagnosticData"}}}};
24857e860f15SJohn Edward Broadbent             });
24865cb1dd27SAsmitha Karunanithi }
24875cb1dd27SAsmitha Karunanithi 
24887e860f15SJohn Edward Broadbent inline void requestRoutesSystemDumpEntryCollection(App& app)
24897e860f15SJohn Edward Broadbent {
24907e860f15SJohn Edward Broadbent 
24915cb1dd27SAsmitha Karunanithi     /**
24925cb1dd27SAsmitha Karunanithi      * Functions triggers appropriate requests on DBus
24935cb1dd27SAsmitha Karunanithi      */
2494b2a3289dSAsmitha Karunanithi     BMCWEB_ROUTE(app, "/redfish/v1/Systems/system/LogServices/Dump/Entries/")
2495ed398213SEd Tanous         .privileges(redfish::privileges::getLogEntryCollection)
24967e860f15SJohn Edward Broadbent         .methods(boost::beast::http::verb::get)(
24977e860f15SJohn Edward Broadbent             [](const crow::Request&,
2498864d6a17SJohn Edward Broadbent                const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) {
24995cb1dd27SAsmitha Karunanithi                 asyncResp->res.jsonValue["@odata.type"] =
25005cb1dd27SAsmitha Karunanithi                     "#LogEntryCollection.LogEntryCollection";
25015cb1dd27SAsmitha Karunanithi                 asyncResp->res.jsonValue["@odata.id"] =
25025cb1dd27SAsmitha Karunanithi                     "/redfish/v1/Systems/system/LogServices/Dump/Entries";
25035cb1dd27SAsmitha Karunanithi                 asyncResp->res.jsonValue["Name"] = "System Dump Entries";
25045cb1dd27SAsmitha Karunanithi                 asyncResp->res.jsonValue["Description"] =
25055cb1dd27SAsmitha Karunanithi                     "Collection of System Dump Entries";
25065cb1dd27SAsmitha Karunanithi 
25075cb1dd27SAsmitha Karunanithi                 getDumpEntryCollection(asyncResp, "System");
25087e860f15SJohn Edward Broadbent             });
25095cb1dd27SAsmitha Karunanithi }
25105cb1dd27SAsmitha Karunanithi 
25117e860f15SJohn Edward Broadbent inline void requestRoutesSystemDumpEntry(App& app)
25125cb1dd27SAsmitha Karunanithi {
25137e860f15SJohn Edward Broadbent     BMCWEB_ROUTE(app,
2514864d6a17SJohn Edward Broadbent                  "/redfish/v1/Systems/system/LogServices/Dump/Entries/<str>/")
2515ed398213SEd Tanous         .privileges(redfish::privileges::getLogEntry)
2516ed398213SEd Tanous 
25177e860f15SJohn Edward Broadbent         .methods(boost::beast::http::verb::get)(
25187e860f15SJohn Edward Broadbent             [](const crow::Request&,
25197e860f15SJohn Edward Broadbent                const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
25207e860f15SJohn Edward Broadbent                const std::string& param) {
25217e860f15SJohn Edward Broadbent                 getDumpEntryById(asyncResp, param, "System");
25227e860f15SJohn Edward Broadbent             });
25238d1b46d7Szhanghch05 
25247e860f15SJohn Edward Broadbent     BMCWEB_ROUTE(app,
2525864d6a17SJohn Edward Broadbent                  "/redfish/v1/Systems/system/LogServices/Dump/Entries/<str>/")
2526ed398213SEd Tanous         .privileges(redfish::privileges::deleteLogEntry)
25277e860f15SJohn Edward Broadbent         .methods(boost::beast::http::verb::delete_)(
25287e860f15SJohn Edward Broadbent             [](const crow::Request&,
25297e860f15SJohn Edward Broadbent                const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
25307e860f15SJohn Edward Broadbent                const std::string& param) {
25317e860f15SJohn Edward Broadbent                 deleteDumpEntry(asyncResp, param, "system");
25327e860f15SJohn Edward Broadbent             });
25335cb1dd27SAsmitha Karunanithi }
2534c9bb6861Sraviteja-b 
25357e860f15SJohn Edward Broadbent inline void requestRoutesSystemDumpCreate(App& app)
2536c9bb6861Sraviteja-b {
25370fda0f12SGeorge Liu     BMCWEB_ROUTE(
25380fda0f12SGeorge Liu         app,
25390fda0f12SGeorge Liu         "/redfish/v1/Systems/system/LogServices/Dump/Actions/LogService.CollectDiagnosticData/")
2540ed398213SEd Tanous         .privileges(redfish::privileges::postLogService)
25417e860f15SJohn Edward Broadbent         .methods(boost::beast::http::verb::post)(
25427e860f15SJohn Edward Broadbent             [](const crow::Request& req,
25437e860f15SJohn Edward Broadbent                const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
25447e860f15SJohn Edward Broadbent 
25457e860f15SJohn Edward Broadbent             { createDump(asyncResp, req, "System"); });
2546a43be80fSAsmitha Karunanithi }
2547a43be80fSAsmitha Karunanithi 
25487e860f15SJohn Edward Broadbent inline void requestRoutesSystemDumpClear(App& app)
2549a43be80fSAsmitha Karunanithi {
25500fda0f12SGeorge Liu     BMCWEB_ROUTE(
25510fda0f12SGeorge Liu         app,
25520fda0f12SGeorge Liu         "/redfish/v1/Systems/system/LogServices/Dump/Actions/LogService.ClearLog/")
2553ed398213SEd Tanous         .privileges(redfish::privileges::postLogService)
25547e860f15SJohn Edward Broadbent         .methods(boost::beast::http::verb::post)(
25557e860f15SJohn Edward Broadbent             [](const crow::Request&,
25567e860f15SJohn Edward Broadbent                const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
25577e860f15SJohn Edward Broadbent 
25587e860f15SJohn Edward Broadbent             { clearDump(asyncResp, "System"); });
2559013487e5Sraviteja-b }
2560013487e5Sraviteja-b 
25617e860f15SJohn Edward Broadbent inline void requestRoutesCrashdumpService(App& app)
25621da66f75SEd Tanous {
25633946028dSAppaRao Puli     // Note: Deviated from redfish privilege registry for GET & HEAD
25643946028dSAppaRao Puli     // method for security reasons.
25651da66f75SEd Tanous     /**
25661da66f75SEd Tanous      * Functions triggers appropriate requests on DBus
25671da66f75SEd Tanous      */
25687e860f15SJohn Edward Broadbent     BMCWEB_ROUTE(app, "/redfish/v1/Systems/system/LogServices/Crashdump/")
2569ed398213SEd Tanous         // This is incorrect, should be:
2570ed398213SEd Tanous         //.privileges(redfish::privileges::getLogService)
2571432a890cSEd Tanous         .privileges({{"ConfigureManager"}})
25727e860f15SJohn Edward Broadbent         .methods(
25737e860f15SJohn Edward Broadbent             boost::beast::http::verb::
25747e860f15SJohn Edward Broadbent                 get)([](const crow::Request&,
25757e860f15SJohn Edward Broadbent                         const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) {
25767e860f15SJohn Edward Broadbent             // Copy over the static data to include the entries added by
25777e860f15SJohn Edward Broadbent             // SubRoute
25780f74e643SEd Tanous             asyncResp->res.jsonValue["@odata.id"] =
2579424c4176SJason M. Bills                 "/redfish/v1/Systems/system/LogServices/Crashdump";
2580e1f26343SJason M. Bills             asyncResp->res.jsonValue["@odata.type"] =
25818e6c099aSJason M. Bills                 "#LogService.v1_2_0.LogService";
25824f50ae4bSGunnar Mills             asyncResp->res.jsonValue["Name"] = "Open BMC Oem Crashdump Service";
25834f50ae4bSGunnar Mills             asyncResp->res.jsonValue["Description"] = "Oem Crashdump Service";
25844f50ae4bSGunnar Mills             asyncResp->res.jsonValue["Id"] = "Oem Crashdump";
2585e1f26343SJason M. Bills             asyncResp->res.jsonValue["OverWritePolicy"] = "WrapsWhenFull";
2586e1f26343SJason M. Bills             asyncResp->res.jsonValue["MaxNumberOfRecords"] = 3;
25877c8c4058STejas Patil 
25887c8c4058STejas Patil             std::pair<std::string, std::string> redfishDateTimeOffset =
25897c8c4058STejas Patil                 crow::utility::getDateTimeOffsetNow();
25907c8c4058STejas Patil             asyncResp->res.jsonValue["DateTime"] = redfishDateTimeOffset.first;
25917c8c4058STejas Patil             asyncResp->res.jsonValue["DateTimeLocalOffset"] =
25927c8c4058STejas Patil                 redfishDateTimeOffset.second;
25937c8c4058STejas Patil 
2594cd50aa42SJason M. Bills             asyncResp->res.jsonValue["Entries"] = {
2595cd50aa42SJason M. Bills                 {"@odata.id",
2596424c4176SJason M. Bills                  "/redfish/v1/Systems/system/LogServices/Crashdump/Entries"}};
2597e1f26343SJason M. Bills             asyncResp->res.jsonValue["Actions"] = {
25985b61b5e8SJason M. Bills                 {"#LogService.ClearLog",
25990fda0f12SGeorge Liu                  {{"target",
26000fda0f12SGeorge Liu                    "/redfish/v1/Systems/system/LogServices/Crashdump/Actions/LogService.ClearLog"}}},
26018e6c099aSJason M. Bills                 {"#LogService.CollectDiagnosticData",
26020fda0f12SGeorge Liu                  {{"target",
26030fda0f12SGeorge Liu                    "/redfish/v1/Systems/system/LogServices/Crashdump/Actions/LogService.CollectDiagnosticData"}}}};
26047e860f15SJohn Edward Broadbent         });
26051da66f75SEd Tanous }
26061da66f75SEd Tanous 
26077e860f15SJohn Edward Broadbent void inline requestRoutesCrashdumpClear(App& app)
26085b61b5e8SJason M. Bills {
26090fda0f12SGeorge Liu     BMCWEB_ROUTE(
26100fda0f12SGeorge Liu         app,
26110fda0f12SGeorge Liu         "/redfish/v1/Systems/system/LogServices/Crashdump/Actions/LogService.ClearLog/")
2612ed398213SEd Tanous         // This is incorrect, should be:
2613ed398213SEd Tanous         //.privileges(redfish::privileges::postLogService)
2614432a890cSEd Tanous         .privileges({{"ConfigureComponents"}})
26157e860f15SJohn Edward Broadbent         .methods(boost::beast::http::verb::post)(
26167e860f15SJohn Edward Broadbent             [](const crow::Request&,
26177e860f15SJohn Edward Broadbent                const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) {
26185b61b5e8SJason M. Bills                 crow::connections::systemBus->async_method_call(
26195b61b5e8SJason M. Bills                     [asyncResp](const boost::system::error_code ec,
2620cb13a392SEd Tanous                                 const std::string&) {
26215b61b5e8SJason M. Bills                         if (ec)
26225b61b5e8SJason M. Bills                         {
26235b61b5e8SJason M. Bills                             messages::internalError(asyncResp->res);
26245b61b5e8SJason M. Bills                             return;
26255b61b5e8SJason M. Bills                         }
26265b61b5e8SJason M. Bills                         messages::success(asyncResp->res);
26275b61b5e8SJason M. Bills                     },
26287e860f15SJohn Edward Broadbent                     crashdumpObject, crashdumpPath, deleteAllInterface,
26297e860f15SJohn Edward Broadbent                     "DeleteAll");
26307e860f15SJohn Edward Broadbent             });
26315b61b5e8SJason M. Bills }
26325b61b5e8SJason M. Bills 
26338d1b46d7Szhanghch05 static void
26348d1b46d7Szhanghch05     logCrashdumpEntry(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
26358d1b46d7Szhanghch05                       const std::string& logID, nlohmann::json& logEntryJson)
2636e855dd28SJason M. Bills {
2637043a0536SJohnathan Mantey     auto getStoredLogCallback =
2638043a0536SJohnathan Mantey         [asyncResp, logID, &logEntryJson](
2639e855dd28SJason M. Bills             const boost::system::error_code ec,
2640168e20c1SEd Tanous             const std::vector<
2641168e20c1SEd Tanous                 std::pair<std::string, dbus::utility::DbusVariantType>>&
2642168e20c1SEd Tanous                 params) {
2643e855dd28SJason M. Bills             if (ec)
2644e855dd28SJason M. Bills             {
2645e855dd28SJason M. Bills                 BMCWEB_LOG_DEBUG << "failed to get log ec: " << ec.message();
26461ddcf01aSJason M. Bills                 if (ec.value() ==
26471ddcf01aSJason M. Bills                     boost::system::linux_error::bad_request_descriptor)
26481ddcf01aSJason M. Bills                 {
2649043a0536SJohnathan Mantey                     messages::resourceNotFound(asyncResp->res, "LogEntry",
2650043a0536SJohnathan Mantey                                                logID);
26511ddcf01aSJason M. Bills                 }
26521ddcf01aSJason M. Bills                 else
26531ddcf01aSJason M. Bills                 {
2654e855dd28SJason M. Bills                     messages::internalError(asyncResp->res);
26551ddcf01aSJason M. Bills                 }
2656e855dd28SJason M. Bills                 return;
2657e855dd28SJason M. Bills             }
2658043a0536SJohnathan Mantey 
2659043a0536SJohnathan Mantey             std::string timestamp{};
2660043a0536SJohnathan Mantey             std::string filename{};
2661043a0536SJohnathan Mantey             std::string logfile{};
26622c70f800SEd Tanous             parseCrashdumpParameters(params, filename, timestamp, logfile);
2663043a0536SJohnathan Mantey 
2664043a0536SJohnathan Mantey             if (filename.empty() || timestamp.empty())
2665e855dd28SJason M. Bills             {
2666043a0536SJohnathan Mantey                 messages::resourceMissingAtURI(asyncResp->res, logID);
2667e855dd28SJason M. Bills                 return;
2668e855dd28SJason M. Bills             }
2669e855dd28SJason M. Bills 
2670043a0536SJohnathan Mantey             std::string crashdumpURI =
2671e855dd28SJason M. Bills                 "/redfish/v1/Systems/system/LogServices/Crashdump/Entries/" +
2672043a0536SJohnathan Mantey                 logID + "/" + filename;
2673d0dbeefdSEd Tanous             logEntryJson = {{"@odata.type", "#LogEntry.v1_7_0.LogEntry"},
2674043a0536SJohnathan Mantey                             {"@odata.id", "/redfish/v1/Systems/system/"
2675043a0536SJohnathan Mantey                                           "LogServices/Crashdump/Entries/" +
2676e855dd28SJason M. Bills                                               logID},
2677e855dd28SJason M. Bills                             {"Name", "CPU Crashdump"},
2678e855dd28SJason M. Bills                             {"Id", logID},
2679e855dd28SJason M. Bills                             {"EntryType", "Oem"},
26808e6c099aSJason M. Bills                             {"AdditionalDataURI", std::move(crashdumpURI)},
26818e6c099aSJason M. Bills                             {"DiagnosticDataType", "OEM"},
26828e6c099aSJason M. Bills                             {"OEMDiagnosticDataType", "PECICrashdump"},
2683043a0536SJohnathan Mantey                             {"Created", std::move(timestamp)}};
2684e855dd28SJason M. Bills         };
2685e855dd28SJason M. Bills     crow::connections::systemBus->async_method_call(
26865b61b5e8SJason M. Bills         std::move(getStoredLogCallback), crashdumpObject,
26875b61b5e8SJason M. Bills         crashdumpPath + std::string("/") + logID,
2688043a0536SJohnathan Mantey         "org.freedesktop.DBus.Properties", "GetAll", crashdumpInterface);
2689e855dd28SJason M. Bills }
2690e855dd28SJason M. Bills 
26917e860f15SJohn Edward Broadbent inline void requestRoutesCrashdumpEntryCollection(App& app)
26921da66f75SEd Tanous {
26933946028dSAppaRao Puli     // Note: Deviated from redfish privilege registry for GET & HEAD
26943946028dSAppaRao Puli     // method for security reasons.
26951da66f75SEd Tanous     /**
26961da66f75SEd Tanous      * Functions triggers appropriate requests on DBus
26971da66f75SEd Tanous      */
26987e860f15SJohn Edward Broadbent     BMCWEB_ROUTE(app,
26997e860f15SJohn Edward Broadbent                  "/redfish/v1/Systems/system/LogServices/Crashdump/Entries/")
2700ed398213SEd Tanous         // This is incorrect, should be.
2701ed398213SEd Tanous         //.privileges(redfish::privileges::postLogEntryCollection)
2702432a890cSEd Tanous         .privileges({{"ConfigureComponents"}})
27037e860f15SJohn Edward Broadbent         .methods(
27047e860f15SJohn Edward Broadbent             boost::beast::http::verb::
27057e860f15SJohn Edward Broadbent                 get)([](const crow::Request&,
27067e860f15SJohn Edward Broadbent                         const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) {
27077e860f15SJohn Edward Broadbent             // Collections don't include the static data added by SubRoute
27087e860f15SJohn Edward Broadbent             // because it has a duplicate entry for members
2709e1f26343SJason M. Bills             auto getLogEntriesCallback = [asyncResp](
2710e1f26343SJason M. Bills                                              const boost::system::error_code ec,
27117e860f15SJohn Edward Broadbent                                              const std::vector<std::string>&
27127e860f15SJohn Edward Broadbent                                                  resp) {
27131da66f75SEd Tanous                 if (ec)
27141da66f75SEd Tanous                 {
27151da66f75SEd Tanous                     if (ec.value() !=
27161da66f75SEd Tanous                         boost::system::errc::no_such_file_or_directory)
27171da66f75SEd Tanous                     {
27181da66f75SEd Tanous                         BMCWEB_LOG_DEBUG << "failed to get entries ec: "
27191da66f75SEd Tanous                                          << ec.message();
2720f12894f8SJason M. Bills                         messages::internalError(asyncResp->res);
27211da66f75SEd Tanous                         return;
27221da66f75SEd Tanous                     }
27231da66f75SEd Tanous                 }
2724e1f26343SJason M. Bills                 asyncResp->res.jsonValue["@odata.type"] =
27251da66f75SEd Tanous                     "#LogEntryCollection.LogEntryCollection";
27260f74e643SEd Tanous                 asyncResp->res.jsonValue["@odata.id"] =
2727424c4176SJason M. Bills                     "/redfish/v1/Systems/system/LogServices/Crashdump/Entries";
2728424c4176SJason M. Bills                 asyncResp->res.jsonValue["Name"] = "Open BMC Crashdump Entries";
2729e1f26343SJason M. Bills                 asyncResp->res.jsonValue["Description"] =
2730424c4176SJason M. Bills                     "Collection of Crashdump Entries";
27317e860f15SJohn Edward Broadbent                 nlohmann::json& logEntryArray =
27327e860f15SJohn Edward Broadbent                     asyncResp->res.jsonValue["Members"];
2733e1f26343SJason M. Bills                 logEntryArray = nlohmann::json::array();
2734e855dd28SJason M. Bills                 std::vector<std::string> logIDs;
2735e855dd28SJason M. Bills                 // Get the list of log entries and build up an empty array big
2736e855dd28SJason M. Bills                 // enough to hold them
27371da66f75SEd Tanous                 for (const std::string& objpath : resp)
27381da66f75SEd Tanous                 {
2739e855dd28SJason M. Bills                     // Get the log ID
2740f23b7296SEd Tanous                     std::size_t lastPos = objpath.rfind('/');
2741e855dd28SJason M. Bills                     if (lastPos == std::string::npos)
27421da66f75SEd Tanous                     {
2743e855dd28SJason M. Bills                         continue;
27441da66f75SEd Tanous                     }
2745e855dd28SJason M. Bills                     logIDs.emplace_back(objpath.substr(lastPos + 1));
2746e855dd28SJason M. Bills 
2747e855dd28SJason M. Bills                     // Add a space for the log entry to the array
2748e855dd28SJason M. Bills                     logEntryArray.push_back({});
2749e855dd28SJason M. Bills                 }
2750e855dd28SJason M. Bills                 // Now go through and set up async calls to fill in the entries
2751e855dd28SJason M. Bills                 size_t index = 0;
2752e855dd28SJason M. Bills                 for (const std::string& logID : logIDs)
2753e855dd28SJason M. Bills                 {
2754e855dd28SJason M. Bills                     // Add the log entry to the array
2755e855dd28SJason M. Bills                     logCrashdumpEntry(asyncResp, logID, logEntryArray[index++]);
27561da66f75SEd Tanous                 }
2757e1f26343SJason M. Bills                 asyncResp->res.jsonValue["Members@odata.count"] =
2758e1f26343SJason M. Bills                     logEntryArray.size();
27591da66f75SEd Tanous             };
27601da66f75SEd Tanous             crow::connections::systemBus->async_method_call(
27611da66f75SEd Tanous                 std::move(getLogEntriesCallback),
27621da66f75SEd Tanous                 "xyz.openbmc_project.ObjectMapper",
27631da66f75SEd Tanous                 "/xyz/openbmc_project/object_mapper",
27641da66f75SEd Tanous                 "xyz.openbmc_project.ObjectMapper", "GetSubTreePaths", "", 0,
27655b61b5e8SJason M. Bills                 std::array<const char*, 1>{crashdumpInterface});
27667e860f15SJohn Edward Broadbent         });
27671da66f75SEd Tanous }
27681da66f75SEd Tanous 
27697e860f15SJohn Edward Broadbent inline void requestRoutesCrashdumpEntry(App& app)
27701da66f75SEd Tanous {
27713946028dSAppaRao Puli     // Note: Deviated from redfish privilege registry for GET & HEAD
27723946028dSAppaRao Puli     // method for security reasons.
27731da66f75SEd Tanous 
27747e860f15SJohn Edward Broadbent     BMCWEB_ROUTE(
27757e860f15SJohn Edward Broadbent         app, "/redfish/v1/Systems/system/LogServices/Crashdump/Entries/<str>/")
2776ed398213SEd Tanous         // this is incorrect, should be
2777ed398213SEd Tanous         // .privileges(redfish::privileges::getLogEntry)
2778432a890cSEd Tanous         .privileges({{"ConfigureComponents"}})
27797e860f15SJohn Edward Broadbent         .methods(boost::beast::http::verb::get)(
27807e860f15SJohn Edward Broadbent             [](const crow::Request&,
27817e860f15SJohn Edward Broadbent                const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
27827e860f15SJohn Edward Broadbent                const std::string& param) {
27837e860f15SJohn Edward Broadbent                 const std::string& logID = param;
2784e855dd28SJason M. Bills                 logCrashdumpEntry(asyncResp, logID, asyncResp->res.jsonValue);
27857e860f15SJohn Edward Broadbent             });
2786e855dd28SJason M. Bills }
2787e855dd28SJason M. Bills 
27887e860f15SJohn Edward Broadbent inline void requestRoutesCrashdumpFile(App& app)
2789e855dd28SJason M. Bills {
27903946028dSAppaRao Puli     // Note: Deviated from redfish privilege registry for GET & HEAD
27913946028dSAppaRao Puli     // method for security reasons.
27927e860f15SJohn Edward Broadbent     BMCWEB_ROUTE(
27937e860f15SJohn Edward Broadbent         app,
27947e860f15SJohn Edward Broadbent         "/redfish/v1/Systems/system/LogServices/Crashdump/Entries/<str>/<str>/")
2795ed398213SEd Tanous         .privileges(redfish::privileges::getLogEntry)
27967e860f15SJohn Edward Broadbent         .methods(boost::beast::http::verb::get)(
27977e860f15SJohn Edward Broadbent             [](const crow::Request&,
27987e860f15SJohn Edward Broadbent                const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
27997e860f15SJohn Edward Broadbent                const std::string& logID, const std::string& fileName) {
2800043a0536SJohnathan Mantey                 auto getStoredLogCallback =
2801043a0536SJohnathan Mantey                     [asyncResp, logID, fileName](
2802abf2add6SEd Tanous                         const boost::system::error_code ec,
2803168e20c1SEd Tanous                         const std::vector<std::pair<
2804168e20c1SEd Tanous                             std::string, dbus::utility::DbusVariantType>>&
28057e860f15SJohn Edward Broadbent                             resp) {
28061da66f75SEd Tanous                         if (ec)
28071da66f75SEd Tanous                         {
2808043a0536SJohnathan Mantey                             BMCWEB_LOG_DEBUG << "failed to get log ec: "
2809043a0536SJohnathan Mantey                                              << ec.message();
2810f12894f8SJason M. Bills                             messages::internalError(asyncResp->res);
28111da66f75SEd Tanous                             return;
28121da66f75SEd Tanous                         }
2813e855dd28SJason M. Bills 
2814043a0536SJohnathan Mantey                         std::string dbusFilename{};
2815043a0536SJohnathan Mantey                         std::string dbusTimestamp{};
2816043a0536SJohnathan Mantey                         std::string dbusFilepath{};
2817043a0536SJohnathan Mantey 
28187e860f15SJohn Edward Broadbent                         parseCrashdumpParameters(resp, dbusFilename,
28197e860f15SJohn Edward Broadbent                                                  dbusTimestamp, dbusFilepath);
2820043a0536SJohnathan Mantey 
2821043a0536SJohnathan Mantey                         if (dbusFilename.empty() || dbusTimestamp.empty() ||
2822043a0536SJohnathan Mantey                             dbusFilepath.empty())
28231da66f75SEd Tanous                         {
28247e860f15SJohn Edward Broadbent                             messages::resourceMissingAtURI(asyncResp->res,
28257e860f15SJohn Edward Broadbent                                                            fileName);
28261da66f75SEd Tanous                             return;
28271da66f75SEd Tanous                         }
2828e855dd28SJason M. Bills 
2829043a0536SJohnathan Mantey                         // Verify the file name parameter is correct
2830043a0536SJohnathan Mantey                         if (fileName != dbusFilename)
2831043a0536SJohnathan Mantey                         {
28327e860f15SJohn Edward Broadbent                             messages::resourceMissingAtURI(asyncResp->res,
28337e860f15SJohn Edward Broadbent                                                            fileName);
2834043a0536SJohnathan Mantey                             return;
2835043a0536SJohnathan Mantey                         }
2836043a0536SJohnathan Mantey 
2837043a0536SJohnathan Mantey                         if (!std::filesystem::exists(dbusFilepath))
2838043a0536SJohnathan Mantey                         {
28397e860f15SJohn Edward Broadbent                             messages::resourceMissingAtURI(asyncResp->res,
28407e860f15SJohn Edward Broadbent                                                            fileName);
2841043a0536SJohnathan Mantey                             return;
2842043a0536SJohnathan Mantey                         }
2843043a0536SJohnathan Mantey                         std::ifstream ifs(dbusFilepath, std::ios::in |
2844043a0536SJohnathan Mantey                                                             std::ios::binary |
2845043a0536SJohnathan Mantey                                                             std::ios::ate);
2846043a0536SJohnathan Mantey                         std::ifstream::pos_type fileSize = ifs.tellg();
2847043a0536SJohnathan Mantey                         if (fileSize < 0)
2848043a0536SJohnathan Mantey                         {
2849043a0536SJohnathan Mantey                             messages::generalError(asyncResp->res);
2850043a0536SJohnathan Mantey                             return;
2851043a0536SJohnathan Mantey                         }
2852043a0536SJohnathan Mantey                         ifs.seekg(0, std::ios::beg);
2853043a0536SJohnathan Mantey 
2854043a0536SJohnathan Mantey                         auto crashData = std::make_unique<char[]>(
2855043a0536SJohnathan Mantey                             static_cast<unsigned int>(fileSize));
2856043a0536SJohnathan Mantey 
2857043a0536SJohnathan Mantey                         ifs.read(crashData.get(), static_cast<int>(fileSize));
2858043a0536SJohnathan Mantey 
28597e860f15SJohn Edward Broadbent                         // The cast to std::string is intentional in order to
28607e860f15SJohn Edward Broadbent                         // use the assign() that applies move mechanics
2861043a0536SJohnathan Mantey                         asyncResp->res.body().assign(
2862043a0536SJohnathan Mantey                             static_cast<std::string>(crashData.get()));
2863043a0536SJohnathan Mantey 
28647e860f15SJohn Edward Broadbent                         // Configure this to be a file download when accessed
28657e860f15SJohn Edward Broadbent                         // from a browser
28667e860f15SJohn Edward Broadbent                         asyncResp->res.addHeader("Content-Disposition",
28677e860f15SJohn Edward Broadbent                                                  "attachment");
28681da66f75SEd Tanous                     };
28691da66f75SEd Tanous                 crow::connections::systemBus->async_method_call(
28705b61b5e8SJason M. Bills                     std::move(getStoredLogCallback), crashdumpObject,
28715b61b5e8SJason M. Bills                     crashdumpPath + std::string("/") + logID,
28727e860f15SJohn Edward Broadbent                     "org.freedesktop.DBus.Properties", "GetAll",
28737e860f15SJohn Edward Broadbent                     crashdumpInterface);
28747e860f15SJohn Edward Broadbent             });
28751da66f75SEd Tanous }
28761da66f75SEd Tanous 
28777e860f15SJohn Edward Broadbent inline void requestRoutesCrashdumpCollect(App& app)
28781da66f75SEd Tanous {
28793946028dSAppaRao Puli     // Note: Deviated from redfish privilege registry for GET & HEAD
28803946028dSAppaRao Puli     // method for security reasons.
28810fda0f12SGeorge Liu     BMCWEB_ROUTE(
28820fda0f12SGeorge Liu         app,
28830fda0f12SGeorge Liu         "/redfish/v1/Systems/system/LogServices/Crashdump/Actions/LogService.CollectDiagnosticData/")
2884ed398213SEd Tanous         // The below is incorrect;  Should be ConfigureManager
2885ed398213SEd Tanous         //.privileges(redfish::privileges::postLogService)
2886432a890cSEd Tanous         .privileges({{"ConfigureComponents"}})
28877e860f15SJohn Edward Broadbent         .methods(
28887e860f15SJohn Edward Broadbent             boost::beast::http::verb::
28897e860f15SJohn Edward Broadbent                 post)([](const crow::Request& req,
28907e860f15SJohn Edward Broadbent                          const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) {
28918e6c099aSJason M. Bills             std::string diagnosticDataType;
28928e6c099aSJason M. Bills             std::string oemDiagnosticDataType;
28938e6c099aSJason M. Bills             if (!redfish::json_util::readJson(
28947e860f15SJohn Edward Broadbent                     req, asyncResp->res, "DiagnosticDataType",
28957e860f15SJohn Edward Broadbent                     diagnosticDataType, "OEMDiagnosticDataType",
28967e860f15SJohn Edward Broadbent                     oemDiagnosticDataType))
28978e6c099aSJason M. Bills             {
28988e6c099aSJason M. Bills                 return;
28998e6c099aSJason M. Bills             }
29008e6c099aSJason M. Bills 
29018e6c099aSJason M. Bills             if (diagnosticDataType != "OEM")
29028e6c099aSJason M. Bills             {
29038e6c099aSJason M. Bills                 BMCWEB_LOG_ERROR
29048e6c099aSJason M. Bills                     << "Only OEM DiagnosticDataType supported for Crashdump";
29058e6c099aSJason M. Bills                 messages::actionParameterValueFormatError(
29068e6c099aSJason M. Bills                     asyncResp->res, diagnosticDataType, "DiagnosticDataType",
29078e6c099aSJason M. Bills                     "CollectDiagnosticData");
29088e6c099aSJason M. Bills                 return;
29098e6c099aSJason M. Bills             }
29108e6c099aSJason M. Bills 
291198be3e39SEd Tanous             auto collectCrashdumpCallback = [asyncResp,
291298be3e39SEd Tanous                                              payload(task::Payload(req))](
29137e860f15SJohn Edward Broadbent                                                 const boost::system::error_code
29147e860f15SJohn Edward Broadbent                                                     ec,
291598be3e39SEd Tanous                                                 const std::string&) mutable {
29161da66f75SEd Tanous                 if (ec)
29171da66f75SEd Tanous                 {
29187e860f15SJohn Edward Broadbent                     if (ec.value() ==
29197e860f15SJohn Edward Broadbent                         boost::system::errc::operation_not_supported)
29201da66f75SEd Tanous                     {
2921f12894f8SJason M. Bills                         messages::resourceInStandby(asyncResp->res);
29221da66f75SEd Tanous                     }
29234363d3b2SJason M. Bills                     else if (ec.value() ==
29244363d3b2SJason M. Bills                              boost::system::errc::device_or_resource_busy)
29254363d3b2SJason M. Bills                     {
29264363d3b2SJason M. Bills                         messages::serviceTemporarilyUnavailable(asyncResp->res,
29274363d3b2SJason M. Bills                                                                 "60");
29284363d3b2SJason M. Bills                     }
29291da66f75SEd Tanous                     else
29301da66f75SEd Tanous                     {
2931f12894f8SJason M. Bills                         messages::internalError(asyncResp->res);
29321da66f75SEd Tanous                     }
29331da66f75SEd Tanous                     return;
29341da66f75SEd Tanous                 }
29350fda0f12SGeorge Liu                 std::shared_ptr<task::TaskData> task = task::TaskData::createTask(
29367e860f15SJohn Edward Broadbent                     [](boost::system::error_code err,
29377e860f15SJohn Edward Broadbent                        sdbusplus::message::message&,
293866afe4faSJames Feist                        const std::shared_ptr<task::TaskData>& taskData) {
293966afe4faSJames Feist                         if (!err)
294066afe4faSJames Feist                         {
2941e5d5006bSJames Feist                             taskData->messages.emplace_back(
2942e5d5006bSJames Feist                                 messages::taskCompletedOK(
2943e5d5006bSJames Feist                                     std::to_string(taskData->index)));
2944831d6b09SJames Feist                             taskData->state = "Completed";
294566afe4faSJames Feist                         }
294632898ceaSJames Feist                         return task::completed;
294766afe4faSJames Feist                     },
29487e860f15SJohn Edward Broadbent                     "type='signal',interface='org.freedesktop.DBus."
29497e860f15SJohn Edward Broadbent                     "Properties',"
29500fda0f12SGeorge Liu                     "member='PropertiesChanged',arg0namespace='com.intel.crashdump'");
295146229577SJames Feist                 task->startTimer(std::chrono::minutes(5));
295246229577SJames Feist                 task->populateResp(asyncResp->res);
295398be3e39SEd Tanous                 task->payload.emplace(std::move(payload));
29541da66f75SEd Tanous             };
29558e6c099aSJason M. Bills 
29568e6c099aSJason M. Bills             if (oemDiagnosticDataType == "OnDemand")
29578e6c099aSJason M. Bills             {
29581da66f75SEd Tanous                 crow::connections::systemBus->async_method_call(
29598e6c099aSJason M. Bills                     std::move(collectCrashdumpCallback), crashdumpObject,
29608e6c099aSJason M. Bills                     crashdumpPath, crashdumpOnDemandInterface,
29618e6c099aSJason M. Bills                     "GenerateOnDemandLog");
29621da66f75SEd Tanous             }
29638e6c099aSJason M. Bills             else if (oemDiagnosticDataType == "Telemetry")
29646eda7685SKenny L. Ku             {
29658e6c099aSJason M. Bills                 crow::connections::systemBus->async_method_call(
29668e6c099aSJason M. Bills                     std::move(collectCrashdumpCallback), crashdumpObject,
29678e6c099aSJason M. Bills                     crashdumpPath, crashdumpTelemetryInterface,
29688e6c099aSJason M. Bills                     "GenerateTelemetryLog");
29696eda7685SKenny L. Ku             }
29706eda7685SKenny L. Ku             else
29716eda7685SKenny L. Ku             {
29728e6c099aSJason M. Bills                 BMCWEB_LOG_ERROR << "Unsupported OEMDiagnosticDataType: "
29738e6c099aSJason M. Bills                                  << oemDiagnosticDataType;
29748e6c099aSJason M. Bills                 messages::actionParameterValueFormatError(
29757e860f15SJohn Edward Broadbent                     asyncResp->res, oemDiagnosticDataType,
29767e860f15SJohn Edward Broadbent                     "OEMDiagnosticDataType", "CollectDiagnosticData");
29776eda7685SKenny L. Ku                 return;
29786eda7685SKenny L. Ku             }
29797e860f15SJohn Edward Broadbent         });
29806eda7685SKenny L. Ku }
29816eda7685SKenny L. Ku 
2982cb92c03bSAndrew Geissler /**
2983cb92c03bSAndrew Geissler  * DBusLogServiceActionsClear class supports POST method for ClearLog action.
2984cb92c03bSAndrew Geissler  */
29857e860f15SJohn Edward Broadbent inline void requestRoutesDBusLogServiceActionsClear(App& app)
2986cb92c03bSAndrew Geissler {
2987cb92c03bSAndrew Geissler     /**
2988cb92c03bSAndrew Geissler      * Function handles POST method request.
2989cb92c03bSAndrew Geissler      * The Clear Log actions does not require any parameter.The action deletes
2990cb92c03bSAndrew Geissler      * all entries found in the Entries collection for this Log Service.
2991cb92c03bSAndrew Geissler      */
29927e860f15SJohn Edward Broadbent 
29930fda0f12SGeorge Liu     BMCWEB_ROUTE(
29940fda0f12SGeorge Liu         app,
29950fda0f12SGeorge Liu         "/redfish/v1/Systems/system/LogServices/EventLog/Actions/LogService.ClearLog/")
2996ed398213SEd Tanous         .privileges(redfish::privileges::postLogService)
29977e860f15SJohn Edward Broadbent         .methods(boost::beast::http::verb::post)(
29987e860f15SJohn Edward Broadbent             [](const crow::Request&,
29997e860f15SJohn Edward Broadbent                const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) {
3000cb92c03bSAndrew Geissler                 BMCWEB_LOG_DEBUG << "Do delete all entries.";
3001cb92c03bSAndrew Geissler 
3002cb92c03bSAndrew Geissler                 // Process response from Logging service.
30037e860f15SJohn Edward Broadbent                 auto respHandler = [asyncResp](
30047e860f15SJohn Edward Broadbent                                        const boost::system::error_code ec) {
30057e860f15SJohn Edward Broadbent                     BMCWEB_LOG_DEBUG
30067e860f15SJohn Edward Broadbent                         << "doClearLog resp_handler callback: Done";
3007cb92c03bSAndrew Geissler                     if (ec)
3008cb92c03bSAndrew Geissler                     {
3009cb92c03bSAndrew Geissler                         // TODO Handle for specific error code
30107e860f15SJohn Edward Broadbent                         BMCWEB_LOG_ERROR << "doClearLog resp_handler got error "
30117e860f15SJohn Edward Broadbent                                          << ec;
3012cb92c03bSAndrew Geissler                         asyncResp->res.result(
3013cb92c03bSAndrew Geissler                             boost::beast::http::status::internal_server_error);
3014cb92c03bSAndrew Geissler                         return;
3015cb92c03bSAndrew Geissler                     }
3016cb92c03bSAndrew Geissler 
30177e860f15SJohn Edward Broadbent                     asyncResp->res.result(
30187e860f15SJohn Edward Broadbent                         boost::beast::http::status::no_content);
3019cb92c03bSAndrew Geissler                 };
3020cb92c03bSAndrew Geissler 
3021cb92c03bSAndrew Geissler                 // Make call to Logging service to request Clear Log
3022cb92c03bSAndrew Geissler                 crow::connections::systemBus->async_method_call(
30232c70f800SEd Tanous                     respHandler, "xyz.openbmc_project.Logging",
3024cb92c03bSAndrew Geissler                     "/xyz/openbmc_project/logging",
3025cb92c03bSAndrew Geissler                     "xyz.openbmc_project.Collection.DeleteAll", "DeleteAll");
30267e860f15SJohn Edward Broadbent             });
3027cb92c03bSAndrew Geissler }
3028a3316fc6SZhikuiRen 
3029a3316fc6SZhikuiRen /****************************************************
3030a3316fc6SZhikuiRen  * Redfish PostCode interfaces
3031a3316fc6SZhikuiRen  * using DBUS interface: getPostCodesTS
3032a3316fc6SZhikuiRen  ******************************************************/
30337e860f15SJohn Edward Broadbent inline void requestRoutesPostCodesLogService(App& app)
3034a3316fc6SZhikuiRen {
30357e860f15SJohn Edward Broadbent     BMCWEB_ROUTE(app, "/redfish/v1/Systems/system/LogServices/PostCodes/")
3036ed398213SEd Tanous         .privileges(redfish::privileges::getLogService)
30370fda0f12SGeorge Liu         .methods(
30380fda0f12SGeorge Liu             boost::beast::http::verb::
30390fda0f12SGeorge Liu                 get)([](const crow::Request&,
30407e860f15SJohn Edward Broadbent                         const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) {
3041a3316fc6SZhikuiRen             asyncResp->res.jsonValue = {
30427e860f15SJohn Edward Broadbent                 {"@odata.id",
30437e860f15SJohn Edward Broadbent                  "/redfish/v1/Systems/system/LogServices/PostCodes"},
3044a3316fc6SZhikuiRen                 {"@odata.type", "#LogService.v1_1_0.LogService"},
3045a3316fc6SZhikuiRen                 {"Name", "POST Code Log Service"},
3046a3316fc6SZhikuiRen                 {"Description", "POST Code Log Service"},
3047a3316fc6SZhikuiRen                 {"Id", "BIOS POST Code Log"},
3048a3316fc6SZhikuiRen                 {"OverWritePolicy", "WrapsWhenFull"},
3049a3316fc6SZhikuiRen                 {"Entries",
30500fda0f12SGeorge Liu                  {{"@odata.id",
30510fda0f12SGeorge Liu                    "/redfish/v1/Systems/system/LogServices/PostCodes/Entries"}}}};
30527c8c4058STejas Patil 
30537c8c4058STejas Patil             std::pair<std::string, std::string> redfishDateTimeOffset =
30547c8c4058STejas Patil                 crow::utility::getDateTimeOffsetNow();
30550fda0f12SGeorge Liu             asyncResp->res.jsonValue["DateTime"] = redfishDateTimeOffset.first;
30567c8c4058STejas Patil             asyncResp->res.jsonValue["DateTimeLocalOffset"] =
30577c8c4058STejas Patil                 redfishDateTimeOffset.second;
30587c8c4058STejas Patil 
3059a3316fc6SZhikuiRen             asyncResp->res.jsonValue["Actions"]["#LogService.ClearLog"] = {
30607e860f15SJohn Edward Broadbent                 {"target",
30610fda0f12SGeorge Liu                  "/redfish/v1/Systems/system/LogServices/PostCodes/Actions/LogService.ClearLog"}};
30627e860f15SJohn Edward Broadbent         });
3063a3316fc6SZhikuiRen }
3064a3316fc6SZhikuiRen 
30657e860f15SJohn Edward Broadbent inline void requestRoutesPostCodesClear(App& app)
3066a3316fc6SZhikuiRen {
30670fda0f12SGeorge Liu     BMCWEB_ROUTE(
30680fda0f12SGeorge Liu         app,
30690fda0f12SGeorge Liu         "/redfish/v1/Systems/system/LogServices/PostCodes/Actions/LogService.ClearLog/")
3070ed398213SEd Tanous         // The following privilege is incorrect;  It should be ConfigureManager
3071ed398213SEd Tanous         //.privileges(redfish::privileges::postLogService)
3072432a890cSEd Tanous         .privileges({{"ConfigureComponents"}})
30737e860f15SJohn Edward Broadbent         .methods(boost::beast::http::verb::post)(
30747e860f15SJohn Edward Broadbent             [](const crow::Request&,
30757e860f15SJohn Edward Broadbent                const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) {
3076a3316fc6SZhikuiRen                 BMCWEB_LOG_DEBUG << "Do delete all postcodes entries.";
3077a3316fc6SZhikuiRen 
3078a3316fc6SZhikuiRen                 // Make call to post-code service to request clear all
3079a3316fc6SZhikuiRen                 crow::connections::systemBus->async_method_call(
3080a3316fc6SZhikuiRen                     [asyncResp](const boost::system::error_code ec) {
3081a3316fc6SZhikuiRen                         if (ec)
3082a3316fc6SZhikuiRen                         {
3083a3316fc6SZhikuiRen                             // TODO Handle for specific error code
3084a3316fc6SZhikuiRen                             BMCWEB_LOG_ERROR
30857e860f15SJohn Edward Broadbent                                 << "doClearPostCodes resp_handler got error "
30867e860f15SJohn Edward Broadbent                                 << ec;
30877e860f15SJohn Edward Broadbent                             asyncResp->res.result(boost::beast::http::status::
30887e860f15SJohn Edward Broadbent                                                       internal_server_error);
3089a3316fc6SZhikuiRen                             messages::internalError(asyncResp->res);
3090a3316fc6SZhikuiRen                             return;
3091a3316fc6SZhikuiRen                         }
3092a3316fc6SZhikuiRen                     },
309315124765SJonathan Doman                     "xyz.openbmc_project.State.Boot.PostCode0",
309415124765SJonathan Doman                     "/xyz/openbmc_project/State/Boot/PostCode0",
3095a3316fc6SZhikuiRen                     "xyz.openbmc_project.Collection.DeleteAll", "DeleteAll");
30967e860f15SJohn Edward Broadbent             });
3097a3316fc6SZhikuiRen }
3098a3316fc6SZhikuiRen 
3099a3316fc6SZhikuiRen static void fillPostCodeEntry(
31008d1b46d7Szhanghch05     const std::shared_ptr<bmcweb::AsyncResp>& aResp,
31016c9a279eSManojkiran Eda     const boost::container::flat_map<
31026c9a279eSManojkiran Eda         uint64_t, std::tuple<uint64_t, std::vector<uint8_t>>>& postcode,
3103a3316fc6SZhikuiRen     const uint16_t bootIndex, const uint64_t codeIndex = 0,
3104a3316fc6SZhikuiRen     const uint64_t skip = 0, const uint64_t top = 0)
3105a3316fc6SZhikuiRen {
3106a3316fc6SZhikuiRen     // Get the Message from the MessageRegistry
3107a3316fc6SZhikuiRen     const message_registries::Message* message =
31084a0bf539SManojkiran Eda         message_registries::getMessage("OpenBMC.0.2.BIOSPOSTCode");
3109a3316fc6SZhikuiRen 
3110a3316fc6SZhikuiRen     uint64_t currentCodeIndex = 0;
3111a3316fc6SZhikuiRen     nlohmann::json& logEntryArray = aResp->res.jsonValue["Members"];
3112a3316fc6SZhikuiRen 
3113a3316fc6SZhikuiRen     uint64_t firstCodeTimeUs = 0;
31146c9a279eSManojkiran Eda     for (const std::pair<uint64_t, std::tuple<uint64_t, std::vector<uint8_t>>>&
31156c9a279eSManojkiran Eda              code : postcode)
3116a3316fc6SZhikuiRen     {
3117a3316fc6SZhikuiRen         currentCodeIndex++;
3118a3316fc6SZhikuiRen         std::string postcodeEntryID =
3119a3316fc6SZhikuiRen             "B" + std::to_string(bootIndex) + "-" +
3120a3316fc6SZhikuiRen             std::to_string(currentCodeIndex); // 1 based index in EntryID string
3121a3316fc6SZhikuiRen 
3122a3316fc6SZhikuiRen         uint64_t usecSinceEpoch = code.first;
3123a3316fc6SZhikuiRen         uint64_t usTimeOffset = 0;
3124a3316fc6SZhikuiRen 
3125a3316fc6SZhikuiRen         if (1 == currentCodeIndex)
3126a3316fc6SZhikuiRen         { // already incremented
3127a3316fc6SZhikuiRen             firstCodeTimeUs = code.first;
3128a3316fc6SZhikuiRen         }
3129a3316fc6SZhikuiRen         else
3130a3316fc6SZhikuiRen         {
3131a3316fc6SZhikuiRen             usTimeOffset = code.first - firstCodeTimeUs;
3132a3316fc6SZhikuiRen         }
3133a3316fc6SZhikuiRen 
3134a3316fc6SZhikuiRen         // skip if no specific codeIndex is specified and currentCodeIndex does
3135a3316fc6SZhikuiRen         // not fall between top and skip
3136a3316fc6SZhikuiRen         if ((codeIndex == 0) &&
3137a3316fc6SZhikuiRen             (currentCodeIndex <= skip || currentCodeIndex > top))
3138a3316fc6SZhikuiRen         {
3139a3316fc6SZhikuiRen             continue;
3140a3316fc6SZhikuiRen         }
3141a3316fc6SZhikuiRen 
31424e0453b1SGunnar Mills         // skip if a specific codeIndex is specified and does not match the
3143a3316fc6SZhikuiRen         // currentIndex
3144a3316fc6SZhikuiRen         if ((codeIndex > 0) && (currentCodeIndex != codeIndex))
3145a3316fc6SZhikuiRen         {
3146a3316fc6SZhikuiRen             // This is done for simplicity. 1st entry is needed to calculate
3147a3316fc6SZhikuiRen             // time offset. To improve efficiency, one can get to the entry
3148a3316fc6SZhikuiRen             // directly (possibly with flatmap's nth method)
3149a3316fc6SZhikuiRen             continue;
3150a3316fc6SZhikuiRen         }
3151a3316fc6SZhikuiRen 
3152a3316fc6SZhikuiRen         // currentCodeIndex is within top and skip or equal to specified code
3153a3316fc6SZhikuiRen         // index
3154a3316fc6SZhikuiRen 
3155a3316fc6SZhikuiRen         // Get the Created time from the timestamp
3156a3316fc6SZhikuiRen         std::string entryTimeStr;
31571d8782e7SNan Zhou         entryTimeStr =
31581d8782e7SNan Zhou             crow::utility::getDateTimeUint(usecSinceEpoch / 1000 / 1000);
3159a3316fc6SZhikuiRen 
3160a3316fc6SZhikuiRen         // assemble messageArgs: BootIndex, TimeOffset(100us), PostCode(hex)
3161a3316fc6SZhikuiRen         std::ostringstream hexCode;
3162a3316fc6SZhikuiRen         hexCode << "0x" << std::setfill('0') << std::setw(2) << std::hex
31636c9a279eSManojkiran Eda                 << std::get<0>(code.second);
3164a3316fc6SZhikuiRen         std::ostringstream timeOffsetStr;
3165a3316fc6SZhikuiRen         // Set Fixed -Point Notation
3166a3316fc6SZhikuiRen         timeOffsetStr << std::fixed;
3167a3316fc6SZhikuiRen         // Set precision to 4 digits
3168a3316fc6SZhikuiRen         timeOffsetStr << std::setprecision(4);
3169a3316fc6SZhikuiRen         // Add double to stream
3170a3316fc6SZhikuiRen         timeOffsetStr << static_cast<double>(usTimeOffset) / 1000 / 1000;
3171a3316fc6SZhikuiRen         std::vector<std::string> messageArgs = {
3172a3316fc6SZhikuiRen             std::to_string(bootIndex), timeOffsetStr.str(), hexCode.str()};
3173a3316fc6SZhikuiRen 
3174a3316fc6SZhikuiRen         // Get MessageArgs template from message registry
3175a3316fc6SZhikuiRen         std::string msg;
3176a3316fc6SZhikuiRen         if (message != nullptr)
3177a3316fc6SZhikuiRen         {
3178a3316fc6SZhikuiRen             msg = message->message;
3179a3316fc6SZhikuiRen 
3180a3316fc6SZhikuiRen             // fill in this post code value
3181a3316fc6SZhikuiRen             int i = 0;
3182a3316fc6SZhikuiRen             for (const std::string& messageArg : messageArgs)
3183a3316fc6SZhikuiRen             {
3184a3316fc6SZhikuiRen                 std::string argStr = "%" + std::to_string(++i);
3185a3316fc6SZhikuiRen                 size_t argPos = msg.find(argStr);
3186a3316fc6SZhikuiRen                 if (argPos != std::string::npos)
3187a3316fc6SZhikuiRen                 {
3188a3316fc6SZhikuiRen                     msg.replace(argPos, argStr.length(), messageArg);
3189a3316fc6SZhikuiRen                 }
3190a3316fc6SZhikuiRen             }
3191a3316fc6SZhikuiRen         }
3192a3316fc6SZhikuiRen 
3193d4342a92STim Lee         // Get Severity template from message registry
3194d4342a92STim Lee         std::string severity;
3195d4342a92STim Lee         if (message != nullptr)
3196d4342a92STim Lee         {
3197d4342a92STim Lee             severity = message->severity;
3198d4342a92STim Lee         }
3199d4342a92STim Lee 
3200a3316fc6SZhikuiRen         // add to AsyncResp
3201a3316fc6SZhikuiRen         logEntryArray.push_back({});
3202a3316fc6SZhikuiRen         nlohmann::json& bmcLogEntry = logEntryArray.back();
32030fda0f12SGeorge Liu         bmcLogEntry = {
32040fda0f12SGeorge Liu             {"@odata.type", "#LogEntry.v1_8_0.LogEntry"},
32050fda0f12SGeorge Liu             {"@odata.id",
32060fda0f12SGeorge Liu              "/redfish/v1/Systems/system/LogServices/PostCodes/Entries/" +
3207a3316fc6SZhikuiRen                  postcodeEntryID},
3208a3316fc6SZhikuiRen             {"Name", "POST Code Log Entry"},
3209a3316fc6SZhikuiRen             {"Id", postcodeEntryID},
3210a3316fc6SZhikuiRen             {"Message", std::move(msg)},
32114a0bf539SManojkiran Eda             {"MessageId", "OpenBMC.0.2.BIOSPOSTCode"},
3212a3316fc6SZhikuiRen             {"MessageArgs", std::move(messageArgs)},
3213a3316fc6SZhikuiRen             {"EntryType", "Event"},
3214a3316fc6SZhikuiRen             {"Severity", std::move(severity)},
32159c620e21SAsmitha Karunanithi             {"Created", entryTimeStr}};
3216647b3cdcSGeorge Liu         if (!std::get<std::vector<uint8_t>>(code.second).empty())
3217647b3cdcSGeorge Liu         {
3218647b3cdcSGeorge Liu             bmcLogEntry["AdditionalDataURI"] =
3219647b3cdcSGeorge Liu                 "/redfish/v1/Systems/system/LogServices/PostCodes/Entries/" +
3220647b3cdcSGeorge Liu                 postcodeEntryID + "/attachment";
3221647b3cdcSGeorge Liu         }
3222a3316fc6SZhikuiRen     }
3223a3316fc6SZhikuiRen }
3224a3316fc6SZhikuiRen 
32258d1b46d7Szhanghch05 static void getPostCodeForEntry(const std::shared_ptr<bmcweb::AsyncResp>& aResp,
3226a3316fc6SZhikuiRen                                 const uint16_t bootIndex,
3227a3316fc6SZhikuiRen                                 const uint64_t codeIndex)
3228a3316fc6SZhikuiRen {
3229a3316fc6SZhikuiRen     crow::connections::systemBus->async_method_call(
32306c9a279eSManojkiran Eda         [aResp, bootIndex,
32316c9a279eSManojkiran Eda          codeIndex](const boost::system::error_code ec,
32326c9a279eSManojkiran Eda                     const boost::container::flat_map<
32336c9a279eSManojkiran Eda                         uint64_t, std::tuple<uint64_t, std::vector<uint8_t>>>&
32346c9a279eSManojkiran Eda                         postcode) {
3235a3316fc6SZhikuiRen             if (ec)
3236a3316fc6SZhikuiRen             {
3237a3316fc6SZhikuiRen                 BMCWEB_LOG_DEBUG << "DBUS POST CODE PostCode response error";
3238a3316fc6SZhikuiRen                 messages::internalError(aResp->res);
3239a3316fc6SZhikuiRen                 return;
3240a3316fc6SZhikuiRen             }
3241a3316fc6SZhikuiRen 
3242a3316fc6SZhikuiRen             // skip the empty postcode boots
3243a3316fc6SZhikuiRen             if (postcode.empty())
3244a3316fc6SZhikuiRen             {
3245a3316fc6SZhikuiRen                 return;
3246a3316fc6SZhikuiRen             }
3247a3316fc6SZhikuiRen 
3248a3316fc6SZhikuiRen             fillPostCodeEntry(aResp, postcode, bootIndex, codeIndex);
3249a3316fc6SZhikuiRen 
3250a3316fc6SZhikuiRen             aResp->res.jsonValue["Members@odata.count"] =
3251a3316fc6SZhikuiRen                 aResp->res.jsonValue["Members"].size();
3252a3316fc6SZhikuiRen         },
325315124765SJonathan Doman         "xyz.openbmc_project.State.Boot.PostCode0",
325415124765SJonathan Doman         "/xyz/openbmc_project/State/Boot/PostCode0",
3255a3316fc6SZhikuiRen         "xyz.openbmc_project.State.Boot.PostCode", "GetPostCodesWithTimeStamp",
3256a3316fc6SZhikuiRen         bootIndex);
3257a3316fc6SZhikuiRen }
3258a3316fc6SZhikuiRen 
32598d1b46d7Szhanghch05 static void getPostCodeForBoot(const std::shared_ptr<bmcweb::AsyncResp>& aResp,
3260a3316fc6SZhikuiRen                                const uint16_t bootIndex,
3261a3316fc6SZhikuiRen                                const uint16_t bootCount,
3262a3316fc6SZhikuiRen                                const uint64_t entryCount, const uint64_t skip,
3263a3316fc6SZhikuiRen                                const uint64_t top)
3264a3316fc6SZhikuiRen {
3265a3316fc6SZhikuiRen     crow::connections::systemBus->async_method_call(
3266a3316fc6SZhikuiRen         [aResp, bootIndex, bootCount, entryCount, skip,
3267a3316fc6SZhikuiRen          top](const boost::system::error_code ec,
32686c9a279eSManojkiran Eda               const boost::container::flat_map<
32696c9a279eSManojkiran Eda                   uint64_t, std::tuple<uint64_t, std::vector<uint8_t>>>&
32706c9a279eSManojkiran Eda                   postcode) {
3271a3316fc6SZhikuiRen             if (ec)
3272a3316fc6SZhikuiRen             {
3273a3316fc6SZhikuiRen                 BMCWEB_LOG_DEBUG << "DBUS POST CODE PostCode response error";
3274a3316fc6SZhikuiRen                 messages::internalError(aResp->res);
3275a3316fc6SZhikuiRen                 return;
3276a3316fc6SZhikuiRen             }
3277a3316fc6SZhikuiRen 
3278a3316fc6SZhikuiRen             uint64_t endCount = entryCount;
3279a3316fc6SZhikuiRen             if (!postcode.empty())
3280a3316fc6SZhikuiRen             {
3281a3316fc6SZhikuiRen                 endCount = entryCount + postcode.size();
3282a3316fc6SZhikuiRen 
3283a3316fc6SZhikuiRen                 if ((skip < endCount) && ((top + skip) > entryCount))
3284a3316fc6SZhikuiRen                 {
3285a3316fc6SZhikuiRen                     uint64_t thisBootSkip =
3286a3316fc6SZhikuiRen                         std::max(skip, entryCount) - entryCount;
3287a3316fc6SZhikuiRen                     uint64_t thisBootTop =
3288a3316fc6SZhikuiRen                         std::min(top + skip, endCount) - entryCount;
3289a3316fc6SZhikuiRen 
3290a3316fc6SZhikuiRen                     fillPostCodeEntry(aResp, postcode, bootIndex, 0,
3291a3316fc6SZhikuiRen                                       thisBootSkip, thisBootTop);
3292a3316fc6SZhikuiRen                 }
3293a3316fc6SZhikuiRen                 aResp->res.jsonValue["Members@odata.count"] = endCount;
3294a3316fc6SZhikuiRen             }
3295a3316fc6SZhikuiRen 
3296a3316fc6SZhikuiRen             // continue to previous bootIndex
3297a3316fc6SZhikuiRen             if (bootIndex < bootCount)
3298a3316fc6SZhikuiRen             {
3299a3316fc6SZhikuiRen                 getPostCodeForBoot(aResp, static_cast<uint16_t>(bootIndex + 1),
3300a3316fc6SZhikuiRen                                    bootCount, endCount, skip, top);
3301a3316fc6SZhikuiRen             }
3302a3316fc6SZhikuiRen             else
3303a3316fc6SZhikuiRen             {
3304a3316fc6SZhikuiRen                 aResp->res.jsonValue["Members@odata.nextLink"] =
33050fda0f12SGeorge Liu                     "/redfish/v1/Systems/system/LogServices/PostCodes/Entries?$skip=" +
3306a3316fc6SZhikuiRen                     std::to_string(skip + top);
3307a3316fc6SZhikuiRen             }
3308a3316fc6SZhikuiRen         },
330915124765SJonathan Doman         "xyz.openbmc_project.State.Boot.PostCode0",
331015124765SJonathan Doman         "/xyz/openbmc_project/State/Boot/PostCode0",
3311a3316fc6SZhikuiRen         "xyz.openbmc_project.State.Boot.PostCode", "GetPostCodesWithTimeStamp",
3312a3316fc6SZhikuiRen         bootIndex);
3313a3316fc6SZhikuiRen }
3314a3316fc6SZhikuiRen 
33158d1b46d7Szhanghch05 static void
33168d1b46d7Szhanghch05     getCurrentBootNumber(const std::shared_ptr<bmcweb::AsyncResp>& aResp,
3317a3316fc6SZhikuiRen                          const uint64_t skip, const uint64_t top)
3318a3316fc6SZhikuiRen {
3319a3316fc6SZhikuiRen     uint64_t entryCount = 0;
33201e1e598dSJonathan Doman     sdbusplus::asio::getProperty<uint16_t>(
33211e1e598dSJonathan Doman         *crow::connections::systemBus,
33221e1e598dSJonathan Doman         "xyz.openbmc_project.State.Boot.PostCode0",
33231e1e598dSJonathan Doman         "/xyz/openbmc_project/State/Boot/PostCode0",
33241e1e598dSJonathan Doman         "xyz.openbmc_project.State.Boot.PostCode", "CurrentBootCycleCount",
33251e1e598dSJonathan Doman         [aResp, entryCount, skip, top](const boost::system::error_code ec,
33261e1e598dSJonathan Doman                                        const uint16_t bootCount) {
3327a3316fc6SZhikuiRen             if (ec)
3328a3316fc6SZhikuiRen             {
3329a3316fc6SZhikuiRen                 BMCWEB_LOG_DEBUG << "DBUS response error " << ec;
3330a3316fc6SZhikuiRen                 messages::internalError(aResp->res);
3331a3316fc6SZhikuiRen                 return;
3332a3316fc6SZhikuiRen             }
33331e1e598dSJonathan Doman             getPostCodeForBoot(aResp, 1, bootCount, entryCount, skip, top);
33341e1e598dSJonathan Doman         });
3335a3316fc6SZhikuiRen }
3336a3316fc6SZhikuiRen 
33377e860f15SJohn Edward Broadbent inline void requestRoutesPostCodesEntryCollection(App& app)
3338a3316fc6SZhikuiRen {
33397e860f15SJohn Edward Broadbent     BMCWEB_ROUTE(app,
33407e860f15SJohn Edward Broadbent                  "/redfish/v1/Systems/system/LogServices/PostCodes/Entries/")
3341ed398213SEd Tanous         .privileges(redfish::privileges::getLogEntryCollection)
33427e860f15SJohn Edward Broadbent         .methods(boost::beast::http::verb::get)(
33437e860f15SJohn Edward Broadbent             [](const crow::Request& req,
33447e860f15SJohn Edward Broadbent                const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) {
3345a3316fc6SZhikuiRen                 asyncResp->res.jsonValue["@odata.type"] =
3346a3316fc6SZhikuiRen                     "#LogEntryCollection.LogEntryCollection";
3347a3316fc6SZhikuiRen                 asyncResp->res.jsonValue["@odata.id"] =
3348a3316fc6SZhikuiRen                     "/redfish/v1/Systems/system/LogServices/PostCodes/Entries";
3349a3316fc6SZhikuiRen                 asyncResp->res.jsonValue["Name"] = "BIOS POST Code Log Entries";
3350a3316fc6SZhikuiRen                 asyncResp->res.jsonValue["Description"] =
3351a3316fc6SZhikuiRen                     "Collection of POST Code Log Entries";
3352a3316fc6SZhikuiRen                 asyncResp->res.jsonValue["Members"] = nlohmann::json::array();
3353a3316fc6SZhikuiRen                 asyncResp->res.jsonValue["Members@odata.count"] = 0;
3354a3316fc6SZhikuiRen 
3355a3316fc6SZhikuiRen                 uint64_t skip = 0;
3356a3316fc6SZhikuiRen                 uint64_t top = maxEntriesPerPage; // Show max entries by default
33578d1b46d7Szhanghch05                 if (!getSkipParam(asyncResp, req, skip))
3358a3316fc6SZhikuiRen                 {
3359a3316fc6SZhikuiRen                     return;
3360a3316fc6SZhikuiRen                 }
33618d1b46d7Szhanghch05                 if (!getTopParam(asyncResp, req, top))
3362a3316fc6SZhikuiRen                 {
3363a3316fc6SZhikuiRen                     return;
3364a3316fc6SZhikuiRen                 }
3365a3316fc6SZhikuiRen                 getCurrentBootNumber(asyncResp, skip, top);
33667e860f15SJohn Edward Broadbent             });
3367a3316fc6SZhikuiRen }
3368a3316fc6SZhikuiRen 
3369647b3cdcSGeorge Liu /**
3370647b3cdcSGeorge Liu  * @brief Parse post code ID and get the current value and index value
3371647b3cdcSGeorge Liu  *        eg: postCodeID=B1-2, currentValue=1, index=2
3372647b3cdcSGeorge Liu  *
3373647b3cdcSGeorge Liu  * @param[in]  postCodeID     Post Code ID
3374647b3cdcSGeorge Liu  * @param[out] currentValue   Current value
3375647b3cdcSGeorge Liu  * @param[out] index          Index value
3376647b3cdcSGeorge Liu  *
3377647b3cdcSGeorge Liu  * @return bool true if the parsing is successful, false the parsing fails
3378647b3cdcSGeorge Liu  */
3379647b3cdcSGeorge Liu inline static bool parsePostCode(const std::string& postCodeID,
3380647b3cdcSGeorge Liu                                  uint64_t& currentValue, uint16_t& index)
3381647b3cdcSGeorge Liu {
3382647b3cdcSGeorge Liu     std::vector<std::string> split;
3383647b3cdcSGeorge Liu     boost::algorithm::split(split, postCodeID, boost::is_any_of("-"));
3384647b3cdcSGeorge Liu     if (split.size() != 2 || split[0].length() < 2 || split[0].front() != 'B')
3385647b3cdcSGeorge Liu     {
3386647b3cdcSGeorge Liu         return false;
3387647b3cdcSGeorge Liu     }
3388647b3cdcSGeorge Liu 
3389647b3cdcSGeorge Liu     const char* start = split[0].data() + 1;
3390647b3cdcSGeorge Liu     const char* end = split[0].data() + split[0].size();
3391647b3cdcSGeorge Liu     auto [ptrIndex, ecIndex] = std::from_chars(start, end, index);
3392647b3cdcSGeorge Liu 
3393647b3cdcSGeorge Liu     if (ptrIndex != end || ecIndex != std::errc())
3394647b3cdcSGeorge Liu     {
3395647b3cdcSGeorge Liu         return false;
3396647b3cdcSGeorge Liu     }
3397647b3cdcSGeorge Liu 
3398647b3cdcSGeorge Liu     start = split[1].data();
3399647b3cdcSGeorge Liu     end = split[1].data() + split[1].size();
3400647b3cdcSGeorge Liu     auto [ptrValue, ecValue] = std::from_chars(start, end, currentValue);
3401647b3cdcSGeorge Liu     if (ptrValue != end || ecValue != std::errc())
3402647b3cdcSGeorge Liu     {
3403647b3cdcSGeorge Liu         return false;
3404647b3cdcSGeorge Liu     }
3405647b3cdcSGeorge Liu 
3406647b3cdcSGeorge Liu     return true;
3407647b3cdcSGeorge Liu }
3408647b3cdcSGeorge Liu 
3409647b3cdcSGeorge Liu inline void requestRoutesPostCodesEntryAdditionalData(App& app)
3410647b3cdcSGeorge Liu {
34110fda0f12SGeorge Liu     BMCWEB_ROUTE(
34120fda0f12SGeorge Liu         app,
34130fda0f12SGeorge Liu         "/redfish/v1/Systems/system/LogServices/PostCodes/Entries/<str>/attachment/")
3414647b3cdcSGeorge Liu         .privileges(redfish::privileges::getLogEntry)
3415647b3cdcSGeorge Liu         .methods(boost::beast::http::verb::get)(
3416647b3cdcSGeorge Liu             [](const crow::Request& req,
3417647b3cdcSGeorge Liu                const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
3418647b3cdcSGeorge Liu                const std::string& postCodeID) {
3419647b3cdcSGeorge Liu                 if (!http_helpers::isOctetAccepted(
3420647b3cdcSGeorge Liu                         req.getHeaderValue("Accept")))
3421647b3cdcSGeorge Liu                 {
3422647b3cdcSGeorge Liu                     asyncResp->res.result(
3423647b3cdcSGeorge Liu                         boost::beast::http::status::bad_request);
3424647b3cdcSGeorge Liu                     return;
3425647b3cdcSGeorge Liu                 }
3426647b3cdcSGeorge Liu 
3427647b3cdcSGeorge Liu                 uint64_t currentValue = 0;
3428647b3cdcSGeorge Liu                 uint16_t index = 0;
3429647b3cdcSGeorge Liu                 if (!parsePostCode(postCodeID, currentValue, index))
3430647b3cdcSGeorge Liu                 {
3431647b3cdcSGeorge Liu                     messages::resourceNotFound(asyncResp->res, "LogEntry",
3432647b3cdcSGeorge Liu                                                postCodeID);
3433647b3cdcSGeorge Liu                     return;
3434647b3cdcSGeorge Liu                 }
3435647b3cdcSGeorge Liu 
3436647b3cdcSGeorge Liu                 crow::connections::systemBus->async_method_call(
3437647b3cdcSGeorge Liu                     [asyncResp, postCodeID, currentValue](
3438647b3cdcSGeorge Liu                         const boost::system::error_code ec,
3439647b3cdcSGeorge Liu                         const std::vector<std::tuple<
3440647b3cdcSGeorge Liu                             uint64_t, std::vector<uint8_t>>>& postcodes) {
3441647b3cdcSGeorge Liu                         if (ec.value() == EBADR)
3442647b3cdcSGeorge Liu                         {
3443647b3cdcSGeorge Liu                             messages::resourceNotFound(asyncResp->res,
3444647b3cdcSGeorge Liu                                                        "LogEntry", postCodeID);
3445647b3cdcSGeorge Liu                             return;
3446647b3cdcSGeorge Liu                         }
3447647b3cdcSGeorge Liu                         if (ec)
3448647b3cdcSGeorge Liu                         {
3449647b3cdcSGeorge Liu                             BMCWEB_LOG_DEBUG << "DBUS response error " << ec;
3450647b3cdcSGeorge Liu                             messages::internalError(asyncResp->res);
3451647b3cdcSGeorge Liu                             return;
3452647b3cdcSGeorge Liu                         }
3453647b3cdcSGeorge Liu 
3454647b3cdcSGeorge Liu                         size_t value = static_cast<size_t>(currentValue) - 1;
3455647b3cdcSGeorge Liu                         if (value == std::string::npos ||
3456647b3cdcSGeorge Liu                             postcodes.size() < currentValue)
3457647b3cdcSGeorge Liu                         {
3458647b3cdcSGeorge Liu                             BMCWEB_LOG_ERROR << "Wrong currentValue value";
3459647b3cdcSGeorge Liu                             messages::resourceNotFound(asyncResp->res,
3460647b3cdcSGeorge Liu                                                        "LogEntry", postCodeID);
3461647b3cdcSGeorge Liu                             return;
3462647b3cdcSGeorge Liu                         }
3463647b3cdcSGeorge Liu 
3464647b3cdcSGeorge Liu                         auto& [tID, code] = postcodes[value];
3465647b3cdcSGeorge Liu                         if (code.empty())
3466647b3cdcSGeorge Liu                         {
3467647b3cdcSGeorge Liu                             BMCWEB_LOG_INFO << "No found post code data";
3468647b3cdcSGeorge Liu                             messages::resourceNotFound(asyncResp->res,
3469647b3cdcSGeorge Liu                                                        "LogEntry", postCodeID);
3470647b3cdcSGeorge Liu                             return;
3471647b3cdcSGeorge Liu                         }
3472647b3cdcSGeorge Liu 
3473647b3cdcSGeorge Liu                         std::string_view strData(
3474647b3cdcSGeorge Liu                             reinterpret_cast<const char*>(code.data()),
3475647b3cdcSGeorge Liu                             code.size());
3476647b3cdcSGeorge Liu 
3477647b3cdcSGeorge Liu                         asyncResp->res.addHeader("Content-Type",
3478647b3cdcSGeorge Liu                                                  "application/octet-stream");
3479647b3cdcSGeorge Liu                         asyncResp->res.addHeader("Content-Transfer-Encoding",
3480647b3cdcSGeorge Liu                                                  "Base64");
3481647b3cdcSGeorge Liu                         asyncResp->res.body() =
3482647b3cdcSGeorge Liu                             crow::utility::base64encode(strData);
3483647b3cdcSGeorge Liu                     },
3484647b3cdcSGeorge Liu                     "xyz.openbmc_project.State.Boot.PostCode0",
3485647b3cdcSGeorge Liu                     "/xyz/openbmc_project/State/Boot/PostCode0",
3486647b3cdcSGeorge Liu                     "xyz.openbmc_project.State.Boot.PostCode", "GetPostCodes",
3487647b3cdcSGeorge Liu                     index);
3488647b3cdcSGeorge Liu             });
3489647b3cdcSGeorge Liu }
3490647b3cdcSGeorge Liu 
34917e860f15SJohn Edward Broadbent inline void requestRoutesPostCodesEntry(App& app)
3492a3316fc6SZhikuiRen {
34937e860f15SJohn Edward Broadbent     BMCWEB_ROUTE(
34947e860f15SJohn Edward Broadbent         app, "/redfish/v1/Systems/system/LogServices/PostCodes/Entries/<str>/")
3495ed398213SEd Tanous         .privileges(redfish::privileges::getLogEntry)
34967e860f15SJohn Edward Broadbent         .methods(boost::beast::http::verb::get)(
34977e860f15SJohn Edward Broadbent             [](const crow::Request&,
34987e860f15SJohn Edward Broadbent                const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
34997e860f15SJohn Edward Broadbent                const std::string& targetID) {
3500647b3cdcSGeorge Liu                 uint16_t bootIndex = 0;
3501647b3cdcSGeorge Liu                 uint64_t codeIndex = 0;
3502647b3cdcSGeorge Liu                 if (!parsePostCode(targetID, codeIndex, bootIndex))
3503a3316fc6SZhikuiRen                 {
3504a3316fc6SZhikuiRen                     // Requested ID was not found
3505a3316fc6SZhikuiRen                     messages::resourceMissingAtURI(asyncResp->res, targetID);
3506a3316fc6SZhikuiRen                     return;
3507a3316fc6SZhikuiRen                 }
3508a3316fc6SZhikuiRen                 if (bootIndex == 0 || codeIndex == 0)
3509a3316fc6SZhikuiRen                 {
3510a3316fc6SZhikuiRen                     BMCWEB_LOG_DEBUG << "Get Post Code invalid entry string "
35117e860f15SJohn Edward Broadbent                                      << targetID;
3512a3316fc6SZhikuiRen                 }
3513a3316fc6SZhikuiRen 
35147e860f15SJohn Edward Broadbent                 asyncResp->res.jsonValue["@odata.type"] =
35157e860f15SJohn Edward Broadbent                     "#LogEntry.v1_4_0.LogEntry";
3516a3316fc6SZhikuiRen                 asyncResp->res.jsonValue["@odata.id"] =
35170fda0f12SGeorge Liu                     "/redfish/v1/Systems/system/LogServices/PostCodes/Entries";
3518a3316fc6SZhikuiRen                 asyncResp->res.jsonValue["Name"] = "BIOS POST Code Log Entries";
3519a3316fc6SZhikuiRen                 asyncResp->res.jsonValue["Description"] =
3520a3316fc6SZhikuiRen                     "Collection of POST Code Log Entries";
3521a3316fc6SZhikuiRen                 asyncResp->res.jsonValue["Members"] = nlohmann::json::array();
3522a3316fc6SZhikuiRen                 asyncResp->res.jsonValue["Members@odata.count"] = 0;
3523a3316fc6SZhikuiRen 
3524a3316fc6SZhikuiRen                 getPostCodeForEntry(asyncResp, bootIndex, codeIndex);
35257e860f15SJohn Edward Broadbent             });
3526a3316fc6SZhikuiRen }
3527a3316fc6SZhikuiRen 
35281da66f75SEd Tanous } // namespace redfish
3529