11da66f75SEd Tanous /* 21da66f75SEd Tanous // Copyright (c) 2018 Intel Corporation 31da66f75SEd Tanous // 41da66f75SEd Tanous // Licensed under the Apache License, Version 2.0 (the "License"); 51da66f75SEd Tanous // you may not use this file except in compliance with the License. 61da66f75SEd Tanous // You may obtain a copy of the License at 71da66f75SEd Tanous // 81da66f75SEd Tanous // http://www.apache.org/licenses/LICENSE-2.0 91da66f75SEd Tanous // 101da66f75SEd Tanous // Unless required by applicable law or agreed to in writing, software 111da66f75SEd Tanous // distributed under the License is distributed on an "AS IS" BASIS, 121da66f75SEd Tanous // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 131da66f75SEd Tanous // See the License for the specific language governing permissions and 141da66f75SEd Tanous // limitations under the License. 151da66f75SEd Tanous */ 161da66f75SEd Tanous #pragma once 171da66f75SEd Tanous 18b7028ebfSSpencer Ku #include "gzfile.hpp" 19647b3cdcSGeorge Liu #include "http_utility.hpp" 20b7028ebfSSpencer Ku #include "human_sort.hpp" 214851d45dSJason M. Bills #include "registries.hpp" 224851d45dSJason M. Bills #include "registries/base_message_registry.hpp" 234851d45dSJason M. Bills #include "registries/openbmc_message_registry.hpp" 2446229577SJames Feist #include "task.hpp" 251da66f75SEd Tanous 26e1f26343SJason M. Bills #include <systemd/sd-journal.h> 27400fd1fbSAdriana Kobylak #include <unistd.h> 28e1f26343SJason M. Bills 297e860f15SJohn Edward Broadbent #include <app.hpp> 3011ba3979SEd Tanous #include <boost/algorithm/string/classification.hpp> 31400fd1fbSAdriana Kobylak #include <boost/algorithm/string/replace.hpp> 324851d45dSJason M. Bills #include <boost/algorithm/string/split.hpp> 3307c8c20dSEd Tanous #include <boost/beast/http/verb.hpp> 341da66f75SEd Tanous #include <boost/container/flat_map.hpp> 351ddcf01aSJason M. Bills #include <boost/system/linux_error.hpp> 36168e20c1SEd Tanous #include <dbus_utility.hpp> 37cb92c03bSAndrew Geissler #include <error_messages.hpp> 3845ca1b86SEd Tanous #include <query.hpp> 39ed398213SEd Tanous #include <registries/privilege_registry.hpp> 401214b7e7SGunnar Mills 41647b3cdcSGeorge Liu #include <charconv> 424418c7f0SJames Feist #include <filesystem> 4375710de2SXiaochao Ma #include <optional> 4426702d01SEd Tanous #include <span> 45cd225da8SJason M. Bills #include <string_view> 46abf2add6SEd Tanous #include <variant> 471da66f75SEd Tanous 481da66f75SEd Tanous namespace redfish 491da66f75SEd Tanous { 501da66f75SEd Tanous 515b61b5e8SJason M. Bills constexpr char const* crashdumpObject = "com.intel.crashdump"; 525b61b5e8SJason M. Bills constexpr char const* crashdumpPath = "/com/intel/crashdump"; 535b61b5e8SJason M. Bills constexpr char const* crashdumpInterface = "com.intel.crashdump"; 545b61b5e8SJason M. Bills constexpr char const* deleteAllInterface = 555b61b5e8SJason M. Bills "xyz.openbmc_project.Collection.DeleteAll"; 565b61b5e8SJason M. Bills constexpr char const* crashdumpOnDemandInterface = 57424c4176SJason M. Bills "com.intel.crashdump.OnDemand"; 586eda7685SKenny L. Ku constexpr char const* crashdumpTelemetryInterface = 596eda7685SKenny L. Ku "com.intel.crashdump.Telemetry"; 601da66f75SEd Tanous 61fffb8c1fSEd Tanous namespace registries 624851d45dSJason M. Bills { 6326702d01SEd Tanous static const Message* 6426702d01SEd Tanous getMessageFromRegistry(const std::string& messageKey, 6526702d01SEd Tanous const std::span<const MessageEntry> registry) 664851d45dSJason M. Bills { 67002d39b4SEd Tanous std::span<const MessageEntry>::iterator messageIt = 68002d39b4SEd Tanous std::find_if(registry.begin(), registry.end(), 694851d45dSJason M. Bills [&messageKey](const MessageEntry& messageEntry) { 70e662eae8SEd Tanous return std::strcmp(messageEntry.first, messageKey.c_str()) == 0; 714851d45dSJason M. Bills }); 7226702d01SEd Tanous if (messageIt != registry.end()) 734851d45dSJason M. Bills { 744851d45dSJason M. Bills return &messageIt->second; 754851d45dSJason M. Bills } 764851d45dSJason M. Bills 774851d45dSJason M. Bills return nullptr; 784851d45dSJason M. Bills } 794851d45dSJason M. Bills 804851d45dSJason M. Bills static const Message* getMessage(const std::string_view& messageID) 814851d45dSJason M. Bills { 824851d45dSJason M. Bills // Redfish MessageIds are in the form 834851d45dSJason M. Bills // RegistryName.MajorVersion.MinorVersion.MessageKey, so parse it to find 844851d45dSJason M. Bills // the right Message 854851d45dSJason M. Bills std::vector<std::string> fields; 864851d45dSJason M. Bills fields.reserve(4); 874851d45dSJason M. Bills boost::split(fields, messageID, boost::is_any_of(".")); 8802cad96eSEd Tanous const std::string& registryName = fields[0]; 8902cad96eSEd Tanous const std::string& messageKey = fields[3]; 904851d45dSJason M. Bills 914851d45dSJason M. Bills // Find the right registry and check it for the MessageKey 924851d45dSJason M. Bills if (std::string(base::header.registryPrefix) == registryName) 934851d45dSJason M. Bills { 944851d45dSJason M. Bills return getMessageFromRegistry( 9526702d01SEd Tanous messageKey, std::span<const MessageEntry>(base::registry)); 964851d45dSJason M. Bills } 974851d45dSJason M. Bills if (std::string(openbmc::header.registryPrefix) == registryName) 984851d45dSJason M. Bills { 994851d45dSJason M. Bills return getMessageFromRegistry( 10026702d01SEd Tanous messageKey, std::span<const MessageEntry>(openbmc::registry)); 1014851d45dSJason M. Bills } 1024851d45dSJason M. Bills return nullptr; 1034851d45dSJason M. Bills } 104fffb8c1fSEd Tanous } // namespace registries 1054851d45dSJason M. Bills 106f6150403SJames Feist namespace fs = std::filesystem; 1071da66f75SEd Tanous 108cb92c03bSAndrew Geissler inline std::string translateSeverityDbusToRedfish(const std::string& s) 109cb92c03bSAndrew Geissler { 110d4d25793SEd Tanous if ((s == "xyz.openbmc_project.Logging.Entry.Level.Alert") || 111d4d25793SEd Tanous (s == "xyz.openbmc_project.Logging.Entry.Level.Critical") || 112d4d25793SEd Tanous (s == "xyz.openbmc_project.Logging.Entry.Level.Emergency") || 113d4d25793SEd Tanous (s == "xyz.openbmc_project.Logging.Entry.Level.Error")) 114cb92c03bSAndrew Geissler { 115cb92c03bSAndrew Geissler return "Critical"; 116cb92c03bSAndrew Geissler } 1173174e4dfSEd Tanous if ((s == "xyz.openbmc_project.Logging.Entry.Level.Debug") || 118d4d25793SEd Tanous (s == "xyz.openbmc_project.Logging.Entry.Level.Informational") || 119d4d25793SEd Tanous (s == "xyz.openbmc_project.Logging.Entry.Level.Notice")) 120cb92c03bSAndrew Geissler { 121cb92c03bSAndrew Geissler return "OK"; 122cb92c03bSAndrew Geissler } 1233174e4dfSEd Tanous if (s == "xyz.openbmc_project.Logging.Entry.Level.Warning") 124cb92c03bSAndrew Geissler { 125cb92c03bSAndrew Geissler return "Warning"; 126cb92c03bSAndrew Geissler } 127cb92c03bSAndrew Geissler return ""; 128cb92c03bSAndrew Geissler } 129cb92c03bSAndrew Geissler 1307e860f15SJohn Edward Broadbent inline static int getJournalMetadata(sd_journal* journal, 13139e77504SEd Tanous const std::string_view& field, 13239e77504SEd Tanous std::string_view& contents) 13316428a1aSJason M. Bills { 13416428a1aSJason M. Bills const char* data = nullptr; 13516428a1aSJason M. Bills size_t length = 0; 13616428a1aSJason M. Bills int ret = 0; 13716428a1aSJason M. Bills // Get the metadata from the requested field of the journal entry 13846ff87baSEd Tanous // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast) 13946ff87baSEd Tanous const void** dataVoid = reinterpret_cast<const void**>(&data); 14046ff87baSEd Tanous 14146ff87baSEd Tanous ret = sd_journal_get_data(journal, field.data(), dataVoid, &length); 14216428a1aSJason M. Bills if (ret < 0) 14316428a1aSJason M. Bills { 14416428a1aSJason M. Bills return ret; 14516428a1aSJason M. Bills } 14639e77504SEd Tanous contents = std::string_view(data, length); 14716428a1aSJason M. Bills // Only use the content after the "=" character. 14881ce609eSEd Tanous contents.remove_prefix(std::min(contents.find('=') + 1, contents.size())); 14916428a1aSJason M. Bills return ret; 15016428a1aSJason M. Bills } 15116428a1aSJason M. Bills 1527e860f15SJohn Edward Broadbent inline static int getJournalMetadata(sd_journal* journal, 1537e860f15SJohn Edward Broadbent const std::string_view& field, 1547e860f15SJohn Edward Broadbent const int& base, long int& contents) 15516428a1aSJason M. Bills { 15616428a1aSJason M. Bills int ret = 0; 15739e77504SEd Tanous std::string_view metadata; 15816428a1aSJason M. Bills // Get the metadata from the requested field of the journal entry 15916428a1aSJason M. Bills ret = getJournalMetadata(journal, field, metadata); 16016428a1aSJason M. Bills if (ret < 0) 16116428a1aSJason M. Bills { 16216428a1aSJason M. Bills return ret; 16316428a1aSJason M. Bills } 164b01bf299SEd Tanous contents = strtol(metadata.data(), nullptr, base); 16516428a1aSJason M. Bills return ret; 16616428a1aSJason M. Bills } 16716428a1aSJason M. Bills 1687e860f15SJohn Edward Broadbent inline static bool getEntryTimestamp(sd_journal* journal, 1697e860f15SJohn Edward Broadbent std::string& entryTimestamp) 170a3316fc6SZhikuiRen { 171a3316fc6SZhikuiRen int ret = 0; 172a3316fc6SZhikuiRen uint64_t timestamp = 0; 173a3316fc6SZhikuiRen ret = sd_journal_get_realtime_usec(journal, ×tamp); 174a3316fc6SZhikuiRen if (ret < 0) 175a3316fc6SZhikuiRen { 176a3316fc6SZhikuiRen BMCWEB_LOG_ERROR << "Failed to read entry timestamp: " 177a3316fc6SZhikuiRen << strerror(-ret); 178a3316fc6SZhikuiRen return false; 179a3316fc6SZhikuiRen } 1801d8782e7SNan Zhou entryTimestamp = crow::utility::getDateTimeUint(timestamp / 1000 / 1000); 1819c620e21SAsmitha Karunanithi return true; 182a3316fc6SZhikuiRen } 18350b8a43aSEd Tanous 1847e860f15SJohn Edward Broadbent inline static bool getUniqueEntryID(sd_journal* journal, std::string& entryID, 185e85d6b16SJason M. Bills const bool firstEntry = true) 18616428a1aSJason M. Bills { 18716428a1aSJason M. Bills int ret = 0; 18816428a1aSJason M. Bills static uint64_t prevTs = 0; 18916428a1aSJason M. Bills static int index = 0; 190e85d6b16SJason M. Bills if (firstEntry) 191e85d6b16SJason M. Bills { 192e85d6b16SJason M. Bills prevTs = 0; 193e85d6b16SJason M. Bills } 194e85d6b16SJason M. Bills 19516428a1aSJason M. Bills // Get the entry timestamp 19616428a1aSJason M. Bills uint64_t curTs = 0; 19716428a1aSJason M. Bills ret = sd_journal_get_realtime_usec(journal, &curTs); 19816428a1aSJason M. Bills if (ret < 0) 19916428a1aSJason M. Bills { 20016428a1aSJason M. Bills BMCWEB_LOG_ERROR << "Failed to read entry timestamp: " 20116428a1aSJason M. Bills << strerror(-ret); 20216428a1aSJason M. Bills return false; 20316428a1aSJason M. Bills } 20416428a1aSJason M. Bills // If the timestamp isn't unique, increment the index 20516428a1aSJason M. Bills if (curTs == prevTs) 20616428a1aSJason M. Bills { 20716428a1aSJason M. Bills index++; 20816428a1aSJason M. Bills } 20916428a1aSJason M. Bills else 21016428a1aSJason M. Bills { 21116428a1aSJason M. Bills // Otherwise, reset it 21216428a1aSJason M. Bills index = 0; 21316428a1aSJason M. Bills } 21416428a1aSJason M. Bills // Save the timestamp 21516428a1aSJason M. Bills prevTs = curTs; 21616428a1aSJason M. Bills 21716428a1aSJason M. Bills entryID = std::to_string(curTs); 21816428a1aSJason M. Bills if (index > 0) 21916428a1aSJason M. Bills { 22016428a1aSJason M. Bills entryID += "_" + std::to_string(index); 22116428a1aSJason M. Bills } 22216428a1aSJason M. Bills return true; 22316428a1aSJason M. Bills } 22416428a1aSJason M. Bills 225e85d6b16SJason M. Bills static bool getUniqueEntryID(const std::string& logEntry, std::string& entryID, 226e85d6b16SJason M. Bills const bool firstEntry = true) 22795820184SJason M. Bills { 228271584abSEd Tanous static time_t prevTs = 0; 22995820184SJason M. Bills static int index = 0; 230e85d6b16SJason M. Bills if (firstEntry) 231e85d6b16SJason M. Bills { 232e85d6b16SJason M. Bills prevTs = 0; 233e85d6b16SJason M. Bills } 234e85d6b16SJason M. Bills 23595820184SJason M. Bills // Get the entry timestamp 236271584abSEd Tanous std::time_t curTs = 0; 23795820184SJason M. Bills std::tm timeStruct = {}; 23895820184SJason M. Bills std::istringstream entryStream(logEntry); 23995820184SJason M. Bills if (entryStream >> std::get_time(&timeStruct, "%Y-%m-%dT%H:%M:%S")) 24095820184SJason M. Bills { 24195820184SJason M. Bills curTs = std::mktime(&timeStruct); 24295820184SJason M. Bills } 24395820184SJason M. Bills // If the timestamp isn't unique, increment the index 24495820184SJason M. Bills if (curTs == prevTs) 24595820184SJason M. Bills { 24695820184SJason M. Bills index++; 24795820184SJason M. Bills } 24895820184SJason M. Bills else 24995820184SJason M. Bills { 25095820184SJason M. Bills // Otherwise, reset it 25195820184SJason M. Bills index = 0; 25295820184SJason M. Bills } 25395820184SJason M. Bills // Save the timestamp 25495820184SJason M. Bills prevTs = curTs; 25595820184SJason M. Bills 25695820184SJason M. Bills entryID = std::to_string(curTs); 25795820184SJason M. Bills if (index > 0) 25895820184SJason M. Bills { 25995820184SJason M. Bills entryID += "_" + std::to_string(index); 26095820184SJason M. Bills } 26195820184SJason M. Bills return true; 26295820184SJason M. Bills } 26395820184SJason M. Bills 2647e860f15SJohn Edward Broadbent inline static bool 2658d1b46d7Szhanghch05 getTimestampFromID(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp, 2668d1b46d7Szhanghch05 const std::string& entryID, uint64_t& timestamp, 2678d1b46d7Szhanghch05 uint64_t& index) 26816428a1aSJason M. Bills { 26916428a1aSJason M. Bills if (entryID.empty()) 27016428a1aSJason M. Bills { 27116428a1aSJason M. Bills return false; 27216428a1aSJason M. Bills } 27316428a1aSJason M. Bills // Convert the unique ID back to a timestamp to find the entry 27439e77504SEd Tanous std::string_view tsStr(entryID); 27516428a1aSJason M. Bills 27681ce609eSEd Tanous auto underscorePos = tsStr.find('_'); 27771d5d8dbSEd Tanous if (underscorePos != std::string_view::npos) 27816428a1aSJason M. Bills { 27916428a1aSJason M. Bills // Timestamp has an index 28016428a1aSJason M. Bills tsStr.remove_suffix(tsStr.size() - underscorePos); 28139e77504SEd Tanous std::string_view indexStr(entryID); 28216428a1aSJason M. Bills indexStr.remove_prefix(underscorePos + 1); 283c0bd5e4bSEd Tanous auto [ptr, ec] = std::from_chars( 284c0bd5e4bSEd Tanous indexStr.data(), indexStr.data() + indexStr.size(), index); 285c0bd5e4bSEd Tanous if (ec != std::errc()) 28616428a1aSJason M. Bills { 287ace85d60SEd Tanous messages::resourceMissingAtURI( 288ace85d60SEd Tanous asyncResp->res, crow::utility::urlFromPieces(entryID)); 28916428a1aSJason M. Bills return false; 29016428a1aSJason M. Bills } 29116428a1aSJason M. Bills } 29216428a1aSJason M. Bills // Timestamp has no index 293c0bd5e4bSEd Tanous auto [ptr, ec] = 294c0bd5e4bSEd Tanous std::from_chars(tsStr.data(), tsStr.data() + tsStr.size(), timestamp); 295c0bd5e4bSEd Tanous if (ec != std::errc()) 29616428a1aSJason M. Bills { 297ace85d60SEd Tanous messages::resourceMissingAtURI(asyncResp->res, 298ace85d60SEd Tanous crow::utility::urlFromPieces(entryID)); 29916428a1aSJason M. Bills return false; 30016428a1aSJason M. Bills } 30116428a1aSJason M. Bills return true; 30216428a1aSJason M. Bills } 30316428a1aSJason M. Bills 30495820184SJason M. Bills static bool 30595820184SJason M. Bills getRedfishLogFiles(std::vector<std::filesystem::path>& redfishLogFiles) 30695820184SJason M. Bills { 30795820184SJason M. Bills static const std::filesystem::path redfishLogDir = "/var/log"; 30895820184SJason M. Bills static const std::string redfishLogFilename = "redfish"; 30995820184SJason M. Bills 31095820184SJason M. Bills // Loop through the directory looking for redfish log files 31195820184SJason M. Bills for (const std::filesystem::directory_entry& dirEnt : 31295820184SJason M. Bills std::filesystem::directory_iterator(redfishLogDir)) 31395820184SJason M. Bills { 31495820184SJason M. Bills // If we find a redfish log file, save the path 31595820184SJason M. Bills std::string filename = dirEnt.path().filename(); 31611ba3979SEd Tanous if (filename.starts_with(redfishLogFilename)) 31795820184SJason M. Bills { 31895820184SJason M. Bills redfishLogFiles.emplace_back(redfishLogDir / filename); 31995820184SJason M. Bills } 32095820184SJason M. Bills } 32195820184SJason M. Bills // As the log files rotate, they are appended with a ".#" that is higher for 32295820184SJason M. Bills // the older logs. Since we don't expect more than 10 log files, we 32395820184SJason M. Bills // can just sort the list to get them in order from newest to oldest 32495820184SJason M. Bills std::sort(redfishLogFiles.begin(), redfishLogFiles.end()); 32595820184SJason M. Bills 32695820184SJason M. Bills return !redfishLogFiles.empty(); 32795820184SJason M. Bills } 32895820184SJason M. Bills 32921ab404cSNan Zhou static std::string getDumpEntriesPath(const std::string& dumpType) 330fdd26906SClaire Weinan { 331fdd26906SClaire Weinan std::string entriesPath; 332fdd26906SClaire Weinan 333fdd26906SClaire Weinan if (dumpType == "BMC") 334fdd26906SClaire Weinan { 335fdd26906SClaire Weinan entriesPath = "/redfish/v1/Managers/bmc/LogServices/Dump/Entries/"; 336fdd26906SClaire Weinan } 337fdd26906SClaire Weinan else if (dumpType == "FaultLog") 338fdd26906SClaire Weinan { 339fdd26906SClaire Weinan entriesPath = "/redfish/v1/Managers/bmc/LogServices/FaultLog/Entries/"; 340fdd26906SClaire Weinan } 341fdd26906SClaire Weinan else if (dumpType == "System") 342fdd26906SClaire Weinan { 343fdd26906SClaire Weinan entriesPath = "/redfish/v1/Systems/system/LogServices/Dump/Entries/"; 344fdd26906SClaire Weinan } 345fdd26906SClaire Weinan else 346fdd26906SClaire Weinan { 347fdd26906SClaire Weinan BMCWEB_LOG_ERROR << "getDumpEntriesPath() invalid dump type: " 348fdd26906SClaire Weinan << dumpType; 349fdd26906SClaire Weinan } 350fdd26906SClaire Weinan 351fdd26906SClaire Weinan // Returns empty string on error 352fdd26906SClaire Weinan return entriesPath; 353fdd26906SClaire Weinan } 354fdd26906SClaire Weinan 3558d1b46d7Szhanghch05 inline void 3568d1b46d7Szhanghch05 getDumpEntryCollection(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp, 3575cb1dd27SAsmitha Karunanithi const std::string& dumpType) 3585cb1dd27SAsmitha Karunanithi { 359fdd26906SClaire Weinan std::string entriesPath = getDumpEntriesPath(dumpType); 360fdd26906SClaire Weinan if (entriesPath.empty()) 3615cb1dd27SAsmitha Karunanithi { 3625cb1dd27SAsmitha Karunanithi messages::internalError(asyncResp->res); 3635cb1dd27SAsmitha Karunanithi return; 3645cb1dd27SAsmitha Karunanithi } 3655cb1dd27SAsmitha Karunanithi 3665cb1dd27SAsmitha Karunanithi crow::connections::systemBus->async_method_call( 367fdd26906SClaire Weinan [asyncResp, entriesPath, 368711ac7a9SEd Tanous dumpType](const boost::system::error_code ec, 369711ac7a9SEd Tanous dbus::utility::ManagedObjectType& resp) { 3705cb1dd27SAsmitha Karunanithi if (ec) 3715cb1dd27SAsmitha Karunanithi { 3725cb1dd27SAsmitha Karunanithi BMCWEB_LOG_ERROR << "DumpEntry resp_handler got error " << ec; 3735cb1dd27SAsmitha Karunanithi messages::internalError(asyncResp->res); 3745cb1dd27SAsmitha Karunanithi return; 3755cb1dd27SAsmitha Karunanithi } 3765cb1dd27SAsmitha Karunanithi 377fdd26906SClaire Weinan // Remove ending slash 378fdd26906SClaire Weinan std::string odataIdStr = entriesPath; 379fdd26906SClaire Weinan if (!odataIdStr.empty()) 380fdd26906SClaire Weinan { 381fdd26906SClaire Weinan odataIdStr.pop_back(); 382fdd26906SClaire Weinan } 383fdd26906SClaire Weinan 384fdd26906SClaire Weinan asyncResp->res.jsonValue["@odata.type"] = 385fdd26906SClaire Weinan "#LogEntryCollection.LogEntryCollection"; 386fdd26906SClaire Weinan asyncResp->res.jsonValue["@odata.id"] = std::move(odataIdStr); 387fdd26906SClaire Weinan asyncResp->res.jsonValue["Name"] = dumpType + " Dump Entries"; 388fdd26906SClaire Weinan asyncResp->res.jsonValue["Description"] = 389fdd26906SClaire Weinan "Collection of " + dumpType + " Dump Entries"; 390fdd26906SClaire Weinan 3915cb1dd27SAsmitha Karunanithi nlohmann::json& entriesArray = asyncResp->res.jsonValue["Members"]; 3925cb1dd27SAsmitha Karunanithi entriesArray = nlohmann::json::array(); 393b47452b2SAsmitha Karunanithi std::string dumpEntryPath = 394b47452b2SAsmitha Karunanithi "/xyz/openbmc_project/dump/" + 395002d39b4SEd Tanous std::string(boost::algorithm::to_lower_copy(dumpType)) + "/entry/"; 3965cb1dd27SAsmitha Karunanithi 397002d39b4SEd Tanous std::sort(resp.begin(), resp.end(), [](const auto& l, const auto& r) { 398002d39b4SEd Tanous return AlphanumLess<std::string>()(l.first.filename(), 399002d39b4SEd Tanous r.first.filename()); 400565dfb6fSClaire Weinan }); 401565dfb6fSClaire Weinan 4025cb1dd27SAsmitha Karunanithi for (auto& object : resp) 4035cb1dd27SAsmitha Karunanithi { 404b47452b2SAsmitha Karunanithi if (object.first.str.find(dumpEntryPath) == std::string::npos) 4055cb1dd27SAsmitha Karunanithi { 4065cb1dd27SAsmitha Karunanithi continue; 4075cb1dd27SAsmitha Karunanithi } 4081d8782e7SNan Zhou uint64_t timestamp = 0; 4095cb1dd27SAsmitha Karunanithi uint64_t size = 0; 41035440d18SAsmitha Karunanithi std::string dumpStatus; 411433b68b4SJason M. Bills nlohmann::json::object_t thisEntry; 4122dfd18efSEd Tanous 4132dfd18efSEd Tanous std::string entryID = object.first.filename(); 4142dfd18efSEd Tanous if (entryID.empty()) 4155cb1dd27SAsmitha Karunanithi { 4165cb1dd27SAsmitha Karunanithi continue; 4175cb1dd27SAsmitha Karunanithi } 4185cb1dd27SAsmitha Karunanithi 4195cb1dd27SAsmitha Karunanithi for (auto& interfaceMap : object.second) 4205cb1dd27SAsmitha Karunanithi { 421002d39b4SEd Tanous if (interfaceMap.first == "xyz.openbmc_project.Common.Progress") 42235440d18SAsmitha Karunanithi { 4239eb808c1SEd Tanous for (const auto& propertyMap : interfaceMap.second) 42435440d18SAsmitha Karunanithi { 42535440d18SAsmitha Karunanithi if (propertyMap.first == "Status") 42635440d18SAsmitha Karunanithi { 427002d39b4SEd Tanous const auto* status = 428002d39b4SEd Tanous std::get_if<std::string>(&propertyMap.second); 42935440d18SAsmitha Karunanithi if (status == nullptr) 43035440d18SAsmitha Karunanithi { 43135440d18SAsmitha Karunanithi messages::internalError(asyncResp->res); 43235440d18SAsmitha Karunanithi break; 43335440d18SAsmitha Karunanithi } 43435440d18SAsmitha Karunanithi dumpStatus = *status; 43535440d18SAsmitha Karunanithi } 43635440d18SAsmitha Karunanithi } 43735440d18SAsmitha Karunanithi } 438002d39b4SEd Tanous else if (interfaceMap.first == "xyz.openbmc_project.Dump.Entry") 4395cb1dd27SAsmitha Karunanithi { 4405cb1dd27SAsmitha Karunanithi 4415cb1dd27SAsmitha Karunanithi for (auto& propertyMap : interfaceMap.second) 4425cb1dd27SAsmitha Karunanithi { 4435cb1dd27SAsmitha Karunanithi if (propertyMap.first == "Size") 4445cb1dd27SAsmitha Karunanithi { 44555f79e6fSEd Tanous const auto* sizePtr = 4465cb1dd27SAsmitha Karunanithi std::get_if<uint64_t>(&propertyMap.second); 4475cb1dd27SAsmitha Karunanithi if (sizePtr == nullptr) 4485cb1dd27SAsmitha Karunanithi { 4495cb1dd27SAsmitha Karunanithi messages::internalError(asyncResp->res); 4505cb1dd27SAsmitha Karunanithi break; 4515cb1dd27SAsmitha Karunanithi } 4525cb1dd27SAsmitha Karunanithi size = *sizePtr; 4535cb1dd27SAsmitha Karunanithi break; 4545cb1dd27SAsmitha Karunanithi } 4555cb1dd27SAsmitha Karunanithi } 4565cb1dd27SAsmitha Karunanithi } 4575cb1dd27SAsmitha Karunanithi else if (interfaceMap.first == 4585cb1dd27SAsmitha Karunanithi "xyz.openbmc_project.Time.EpochTime") 4595cb1dd27SAsmitha Karunanithi { 4605cb1dd27SAsmitha Karunanithi 4619eb808c1SEd Tanous for (const auto& propertyMap : interfaceMap.second) 4625cb1dd27SAsmitha Karunanithi { 4635cb1dd27SAsmitha Karunanithi if (propertyMap.first == "Elapsed") 4645cb1dd27SAsmitha Karunanithi { 4655cb1dd27SAsmitha Karunanithi const uint64_t* usecsTimeStamp = 4665cb1dd27SAsmitha Karunanithi std::get_if<uint64_t>(&propertyMap.second); 4675cb1dd27SAsmitha Karunanithi if (usecsTimeStamp == nullptr) 4685cb1dd27SAsmitha Karunanithi { 4695cb1dd27SAsmitha Karunanithi messages::internalError(asyncResp->res); 4705cb1dd27SAsmitha Karunanithi break; 4715cb1dd27SAsmitha Karunanithi } 4721d8782e7SNan Zhou timestamp = (*usecsTimeStamp / 1000 / 1000); 4735cb1dd27SAsmitha Karunanithi break; 4745cb1dd27SAsmitha Karunanithi } 4755cb1dd27SAsmitha Karunanithi } 4765cb1dd27SAsmitha Karunanithi } 4775cb1dd27SAsmitha Karunanithi } 4785cb1dd27SAsmitha Karunanithi 4790fda0f12SGeorge Liu if (dumpStatus != 4800fda0f12SGeorge Liu "xyz.openbmc_project.Common.Progress.OperationStatus.Completed" && 48135440d18SAsmitha Karunanithi !dumpStatus.empty()) 48235440d18SAsmitha Karunanithi { 48335440d18SAsmitha Karunanithi // Dump status is not Complete, no need to enumerate 48435440d18SAsmitha Karunanithi continue; 48535440d18SAsmitha Karunanithi } 48635440d18SAsmitha Karunanithi 487647b3cdcSGeorge Liu thisEntry["@odata.type"] = "#LogEntry.v1_8_0.LogEntry"; 488fdd26906SClaire Weinan thisEntry["@odata.id"] = entriesPath + entryID; 4895cb1dd27SAsmitha Karunanithi thisEntry["Id"] = entryID; 4905cb1dd27SAsmitha Karunanithi thisEntry["EntryType"] = "Event"; 491002d39b4SEd Tanous thisEntry["Created"] = crow::utility::getDateTimeUint(timestamp); 4925cb1dd27SAsmitha Karunanithi thisEntry["Name"] = dumpType + " Dump Entry"; 4935cb1dd27SAsmitha Karunanithi 4945cb1dd27SAsmitha Karunanithi if (dumpType == "BMC") 4955cb1dd27SAsmitha Karunanithi { 496d337bb72SAsmitha Karunanithi thisEntry["DiagnosticDataType"] = "Manager"; 497d337bb72SAsmitha Karunanithi thisEntry["AdditionalDataURI"] = 498fdd26906SClaire Weinan entriesPath + entryID + "/attachment"; 499fdd26906SClaire Weinan thisEntry["AdditionalDataSizeBytes"] = size; 5005cb1dd27SAsmitha Karunanithi } 5015cb1dd27SAsmitha Karunanithi else if (dumpType == "System") 5025cb1dd27SAsmitha Karunanithi { 503d337bb72SAsmitha Karunanithi thisEntry["DiagnosticDataType"] = "OEM"; 504d337bb72SAsmitha Karunanithi thisEntry["OEMDiagnosticDataType"] = "System"; 505d337bb72SAsmitha Karunanithi thisEntry["AdditionalDataURI"] = 506fdd26906SClaire Weinan entriesPath + entryID + "/attachment"; 507fdd26906SClaire Weinan thisEntry["AdditionalDataSizeBytes"] = size; 5085cb1dd27SAsmitha Karunanithi } 50935440d18SAsmitha Karunanithi entriesArray.push_back(std::move(thisEntry)); 5105cb1dd27SAsmitha Karunanithi } 511002d39b4SEd Tanous asyncResp->res.jsonValue["Members@odata.count"] = entriesArray.size(); 5125cb1dd27SAsmitha Karunanithi }, 5135cb1dd27SAsmitha Karunanithi "xyz.openbmc_project.Dump.Manager", "/xyz/openbmc_project/dump", 5145cb1dd27SAsmitha Karunanithi "org.freedesktop.DBus.ObjectManager", "GetManagedObjects"); 5155cb1dd27SAsmitha Karunanithi } 5165cb1dd27SAsmitha Karunanithi 5178d1b46d7Szhanghch05 inline void 518c7a6d660SClaire Weinan getDumpEntryById(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp, 5198d1b46d7Szhanghch05 const std::string& entryID, const std::string& dumpType) 5205cb1dd27SAsmitha Karunanithi { 521fdd26906SClaire Weinan std::string entriesPath = getDumpEntriesPath(dumpType); 522fdd26906SClaire Weinan if (entriesPath.empty()) 5235cb1dd27SAsmitha Karunanithi { 5245cb1dd27SAsmitha Karunanithi messages::internalError(asyncResp->res); 5255cb1dd27SAsmitha Karunanithi return; 5265cb1dd27SAsmitha Karunanithi } 5275cb1dd27SAsmitha Karunanithi 5285cb1dd27SAsmitha Karunanithi crow::connections::systemBus->async_method_call( 529fdd26906SClaire Weinan [asyncResp, entryID, dumpType, 530fdd26906SClaire Weinan entriesPath](const boost::system::error_code ec, 53102cad96eSEd Tanous const dbus::utility::ManagedObjectType& resp) { 5325cb1dd27SAsmitha Karunanithi if (ec) 5335cb1dd27SAsmitha Karunanithi { 5345cb1dd27SAsmitha Karunanithi BMCWEB_LOG_ERROR << "DumpEntry resp_handler got error " << ec; 5355cb1dd27SAsmitha Karunanithi messages::internalError(asyncResp->res); 5365cb1dd27SAsmitha Karunanithi return; 5375cb1dd27SAsmitha Karunanithi } 5385cb1dd27SAsmitha Karunanithi 539b47452b2SAsmitha Karunanithi bool foundDumpEntry = false; 540b47452b2SAsmitha Karunanithi std::string dumpEntryPath = 541b47452b2SAsmitha Karunanithi "/xyz/openbmc_project/dump/" + 542002d39b4SEd Tanous std::string(boost::algorithm::to_lower_copy(dumpType)) + "/entry/"; 543b47452b2SAsmitha Karunanithi 5449eb808c1SEd Tanous for (const auto& objectPath : resp) 5455cb1dd27SAsmitha Karunanithi { 546b47452b2SAsmitha Karunanithi if (objectPath.first.str != dumpEntryPath + entryID) 5475cb1dd27SAsmitha Karunanithi { 5485cb1dd27SAsmitha Karunanithi continue; 5495cb1dd27SAsmitha Karunanithi } 5505cb1dd27SAsmitha Karunanithi 5515cb1dd27SAsmitha Karunanithi foundDumpEntry = true; 5521d8782e7SNan Zhou uint64_t timestamp = 0; 5535cb1dd27SAsmitha Karunanithi uint64_t size = 0; 55435440d18SAsmitha Karunanithi std::string dumpStatus; 5555cb1dd27SAsmitha Karunanithi 5569eb808c1SEd Tanous for (const auto& interfaceMap : objectPath.second) 5575cb1dd27SAsmitha Karunanithi { 558002d39b4SEd Tanous if (interfaceMap.first == "xyz.openbmc_project.Common.Progress") 55935440d18SAsmitha Karunanithi { 5609eb808c1SEd Tanous for (const auto& propertyMap : interfaceMap.second) 56135440d18SAsmitha Karunanithi { 56235440d18SAsmitha Karunanithi if (propertyMap.first == "Status") 56335440d18SAsmitha Karunanithi { 5649eb808c1SEd Tanous const std::string* status = 565002d39b4SEd Tanous std::get_if<std::string>(&propertyMap.second); 56635440d18SAsmitha Karunanithi if (status == nullptr) 56735440d18SAsmitha Karunanithi { 56835440d18SAsmitha Karunanithi messages::internalError(asyncResp->res); 56935440d18SAsmitha Karunanithi break; 57035440d18SAsmitha Karunanithi } 57135440d18SAsmitha Karunanithi dumpStatus = *status; 57235440d18SAsmitha Karunanithi } 57335440d18SAsmitha Karunanithi } 57435440d18SAsmitha Karunanithi } 575002d39b4SEd Tanous else if (interfaceMap.first == "xyz.openbmc_project.Dump.Entry") 5765cb1dd27SAsmitha Karunanithi { 5779eb808c1SEd Tanous for (const auto& propertyMap : interfaceMap.second) 5785cb1dd27SAsmitha Karunanithi { 5795cb1dd27SAsmitha Karunanithi if (propertyMap.first == "Size") 5805cb1dd27SAsmitha Karunanithi { 5819eb808c1SEd Tanous const uint64_t* sizePtr = 5825cb1dd27SAsmitha Karunanithi std::get_if<uint64_t>(&propertyMap.second); 5835cb1dd27SAsmitha Karunanithi if (sizePtr == nullptr) 5845cb1dd27SAsmitha Karunanithi { 5855cb1dd27SAsmitha Karunanithi messages::internalError(asyncResp->res); 5865cb1dd27SAsmitha Karunanithi break; 5875cb1dd27SAsmitha Karunanithi } 5885cb1dd27SAsmitha Karunanithi size = *sizePtr; 5895cb1dd27SAsmitha Karunanithi break; 5905cb1dd27SAsmitha Karunanithi } 5915cb1dd27SAsmitha Karunanithi } 5925cb1dd27SAsmitha Karunanithi } 5935cb1dd27SAsmitha Karunanithi else if (interfaceMap.first == 5945cb1dd27SAsmitha Karunanithi "xyz.openbmc_project.Time.EpochTime") 5955cb1dd27SAsmitha Karunanithi { 5969eb808c1SEd Tanous for (const auto& propertyMap : interfaceMap.second) 5975cb1dd27SAsmitha Karunanithi { 5985cb1dd27SAsmitha Karunanithi if (propertyMap.first == "Elapsed") 5995cb1dd27SAsmitha Karunanithi { 6005cb1dd27SAsmitha Karunanithi const uint64_t* usecsTimeStamp = 6015cb1dd27SAsmitha Karunanithi std::get_if<uint64_t>(&propertyMap.second); 6025cb1dd27SAsmitha Karunanithi if (usecsTimeStamp == nullptr) 6035cb1dd27SAsmitha Karunanithi { 6045cb1dd27SAsmitha Karunanithi messages::internalError(asyncResp->res); 6055cb1dd27SAsmitha Karunanithi break; 6065cb1dd27SAsmitha Karunanithi } 6071d8782e7SNan Zhou timestamp = *usecsTimeStamp / 1000 / 1000; 6085cb1dd27SAsmitha Karunanithi break; 6095cb1dd27SAsmitha Karunanithi } 6105cb1dd27SAsmitha Karunanithi } 6115cb1dd27SAsmitha Karunanithi } 6125cb1dd27SAsmitha Karunanithi } 6135cb1dd27SAsmitha Karunanithi 6140fda0f12SGeorge Liu if (dumpStatus != 6150fda0f12SGeorge Liu "xyz.openbmc_project.Common.Progress.OperationStatus.Completed" && 61635440d18SAsmitha Karunanithi !dumpStatus.empty()) 61735440d18SAsmitha Karunanithi { 61835440d18SAsmitha Karunanithi // Dump status is not Complete 61935440d18SAsmitha Karunanithi // return not found until status is changed to Completed 620002d39b4SEd Tanous messages::resourceNotFound(asyncResp->res, dumpType + " dump", 621002d39b4SEd Tanous entryID); 62235440d18SAsmitha Karunanithi return; 62335440d18SAsmitha Karunanithi } 62435440d18SAsmitha Karunanithi 6255cb1dd27SAsmitha Karunanithi asyncResp->res.jsonValue["@odata.type"] = 626647b3cdcSGeorge Liu "#LogEntry.v1_8_0.LogEntry"; 627fdd26906SClaire Weinan asyncResp->res.jsonValue["@odata.id"] = entriesPath + entryID; 6285cb1dd27SAsmitha Karunanithi asyncResp->res.jsonValue["Id"] = entryID; 6295cb1dd27SAsmitha Karunanithi asyncResp->res.jsonValue["EntryType"] = "Event"; 6305cb1dd27SAsmitha Karunanithi asyncResp->res.jsonValue["Created"] = 6311d8782e7SNan Zhou crow::utility::getDateTimeUint(timestamp); 6325cb1dd27SAsmitha Karunanithi asyncResp->res.jsonValue["Name"] = dumpType + " Dump Entry"; 6335cb1dd27SAsmitha Karunanithi 6345cb1dd27SAsmitha Karunanithi if (dumpType == "BMC") 6355cb1dd27SAsmitha Karunanithi { 636d337bb72SAsmitha Karunanithi asyncResp->res.jsonValue["DiagnosticDataType"] = "Manager"; 637d337bb72SAsmitha Karunanithi asyncResp->res.jsonValue["AdditionalDataURI"] = 638fdd26906SClaire Weinan entriesPath + entryID + "/attachment"; 639fdd26906SClaire Weinan asyncResp->res.jsonValue["AdditionalDataSizeBytes"] = size; 6405cb1dd27SAsmitha Karunanithi } 6415cb1dd27SAsmitha Karunanithi else if (dumpType == "System") 6425cb1dd27SAsmitha Karunanithi { 643d337bb72SAsmitha Karunanithi asyncResp->res.jsonValue["DiagnosticDataType"] = "OEM"; 644002d39b4SEd Tanous asyncResp->res.jsonValue["OEMDiagnosticDataType"] = "System"; 645d337bb72SAsmitha Karunanithi asyncResp->res.jsonValue["AdditionalDataURI"] = 646fdd26906SClaire Weinan entriesPath + entryID + "/attachment"; 647fdd26906SClaire Weinan asyncResp->res.jsonValue["AdditionalDataSizeBytes"] = size; 6485cb1dd27SAsmitha Karunanithi } 6495cb1dd27SAsmitha Karunanithi } 650e05aec50SEd Tanous if (!foundDumpEntry) 651b47452b2SAsmitha Karunanithi { 652b47452b2SAsmitha Karunanithi BMCWEB_LOG_ERROR << "Can't find Dump Entry"; 653b47452b2SAsmitha Karunanithi messages::internalError(asyncResp->res); 654b47452b2SAsmitha Karunanithi return; 655b47452b2SAsmitha Karunanithi } 6565cb1dd27SAsmitha Karunanithi }, 6575cb1dd27SAsmitha Karunanithi "xyz.openbmc_project.Dump.Manager", "/xyz/openbmc_project/dump", 6585cb1dd27SAsmitha Karunanithi "org.freedesktop.DBus.ObjectManager", "GetManagedObjects"); 6595cb1dd27SAsmitha Karunanithi } 6605cb1dd27SAsmitha Karunanithi 6618d1b46d7Szhanghch05 inline void deleteDumpEntry(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp, 6629878256fSStanley Chu const std::string& entryID, 663b47452b2SAsmitha Karunanithi const std::string& dumpType) 6645cb1dd27SAsmitha Karunanithi { 665002d39b4SEd Tanous auto respHandler = 666002d39b4SEd Tanous [asyncResp, entryID](const boost::system::error_code ec) { 6675cb1dd27SAsmitha Karunanithi BMCWEB_LOG_DEBUG << "Dump Entry doDelete callback: Done"; 6685cb1dd27SAsmitha Karunanithi if (ec) 6695cb1dd27SAsmitha Karunanithi { 6703de8d8baSGeorge Liu if (ec.value() == EBADR) 6713de8d8baSGeorge Liu { 6723de8d8baSGeorge Liu messages::resourceNotFound(asyncResp->res, "LogEntry", entryID); 6733de8d8baSGeorge Liu return; 6743de8d8baSGeorge Liu } 6755cb1dd27SAsmitha Karunanithi BMCWEB_LOG_ERROR << "Dump (DBus) doDelete respHandler got error " 676fdd26906SClaire Weinan << ec << " entryID=" << entryID; 6775cb1dd27SAsmitha Karunanithi messages::internalError(asyncResp->res); 6785cb1dd27SAsmitha Karunanithi return; 6795cb1dd27SAsmitha Karunanithi } 6805cb1dd27SAsmitha Karunanithi }; 6815cb1dd27SAsmitha Karunanithi crow::connections::systemBus->async_method_call( 6825cb1dd27SAsmitha Karunanithi respHandler, "xyz.openbmc_project.Dump.Manager", 683b47452b2SAsmitha Karunanithi "/xyz/openbmc_project/dump/" + 684b47452b2SAsmitha Karunanithi std::string(boost::algorithm::to_lower_copy(dumpType)) + "/entry/" + 685b47452b2SAsmitha Karunanithi entryID, 6865cb1dd27SAsmitha Karunanithi "xyz.openbmc_project.Object.Delete", "Delete"); 6875cb1dd27SAsmitha Karunanithi } 6885cb1dd27SAsmitha Karunanithi 6898d1b46d7Szhanghch05 inline void 69098be3e39SEd Tanous createDumpTaskCallback(task::Payload&& payload, 6918d1b46d7Szhanghch05 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp, 6928d1b46d7Szhanghch05 const uint32_t& dumpId, const std::string& dumpPath, 693a43be80fSAsmitha Karunanithi const std::string& dumpType) 694a43be80fSAsmitha Karunanithi { 695a43be80fSAsmitha Karunanithi std::shared_ptr<task::TaskData> task = task::TaskData::createTask( 696*59d494eeSPatrick Williams [dumpId, dumpPath, 697*59d494eeSPatrick Williams dumpType](boost::system::error_code err, sdbusplus::message_t& m, 698a43be80fSAsmitha Karunanithi const std::shared_ptr<task::TaskData>& taskData) { 699cb13a392SEd Tanous if (err) 700cb13a392SEd Tanous { 7016145ed6fSAsmitha Karunanithi BMCWEB_LOG_ERROR << "Error in creating a dump"; 7026145ed6fSAsmitha Karunanithi taskData->state = "Cancelled"; 7036145ed6fSAsmitha Karunanithi return task::completed; 704cb13a392SEd Tanous } 705b9d36b47SEd Tanous 706b9d36b47SEd Tanous dbus::utility::DBusInteracesMap interfacesList; 707a43be80fSAsmitha Karunanithi 708a43be80fSAsmitha Karunanithi sdbusplus::message::object_path objPath; 709a43be80fSAsmitha Karunanithi 710a43be80fSAsmitha Karunanithi m.read(objPath, interfacesList); 711a43be80fSAsmitha Karunanithi 712b47452b2SAsmitha Karunanithi if (objPath.str == 713b47452b2SAsmitha Karunanithi "/xyz/openbmc_project/dump/" + 714b47452b2SAsmitha Karunanithi std::string(boost::algorithm::to_lower_copy(dumpType)) + 715b47452b2SAsmitha Karunanithi "/entry/" + std::to_string(dumpId)) 716a43be80fSAsmitha Karunanithi { 717a43be80fSAsmitha Karunanithi nlohmann::json retMessage = messages::success(); 718a43be80fSAsmitha Karunanithi taskData->messages.emplace_back(retMessage); 719a43be80fSAsmitha Karunanithi 720a43be80fSAsmitha Karunanithi std::string headerLoc = 721a43be80fSAsmitha Karunanithi "Location: " + dumpPath + std::to_string(dumpId); 722002d39b4SEd Tanous taskData->payload->httpHeaders.emplace_back(std::move(headerLoc)); 723a43be80fSAsmitha Karunanithi 724a43be80fSAsmitha Karunanithi taskData->state = "Completed"; 725b47452b2SAsmitha Karunanithi return task::completed; 7266145ed6fSAsmitha Karunanithi } 727a43be80fSAsmitha Karunanithi return task::completed; 728a43be80fSAsmitha Karunanithi }, 7294978b63fSJason M. Bills "type='signal',interface='org.freedesktop.DBus.ObjectManager'," 730a43be80fSAsmitha Karunanithi "member='InterfacesAdded', " 731a43be80fSAsmitha Karunanithi "path='/xyz/openbmc_project/dump'"); 732a43be80fSAsmitha Karunanithi 733a43be80fSAsmitha Karunanithi task->startTimer(std::chrono::minutes(3)); 734a43be80fSAsmitha Karunanithi task->populateResp(asyncResp->res); 73598be3e39SEd Tanous task->payload.emplace(std::move(payload)); 736a43be80fSAsmitha Karunanithi } 737a43be80fSAsmitha Karunanithi 7388d1b46d7Szhanghch05 inline void createDump(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp, 7398d1b46d7Szhanghch05 const crow::Request& req, const std::string& dumpType) 740a43be80fSAsmitha Karunanithi { 741fdd26906SClaire Weinan std::string dumpPath = getDumpEntriesPath(dumpType); 742fdd26906SClaire Weinan if (dumpPath.empty()) 743a43be80fSAsmitha Karunanithi { 744a43be80fSAsmitha Karunanithi messages::internalError(asyncResp->res); 745a43be80fSAsmitha Karunanithi return; 746a43be80fSAsmitha Karunanithi } 747a43be80fSAsmitha Karunanithi 748a43be80fSAsmitha Karunanithi std::optional<std::string> diagnosticDataType; 749a43be80fSAsmitha Karunanithi std::optional<std::string> oemDiagnosticDataType; 750a43be80fSAsmitha Karunanithi 75115ed6780SWilly Tu if (!redfish::json_util::readJsonAction( 752a43be80fSAsmitha Karunanithi req, asyncResp->res, "DiagnosticDataType", diagnosticDataType, 753a43be80fSAsmitha Karunanithi "OEMDiagnosticDataType", oemDiagnosticDataType)) 754a43be80fSAsmitha Karunanithi { 755a43be80fSAsmitha Karunanithi return; 756a43be80fSAsmitha Karunanithi } 757a43be80fSAsmitha Karunanithi 758a43be80fSAsmitha Karunanithi if (dumpType == "System") 759a43be80fSAsmitha Karunanithi { 760a43be80fSAsmitha Karunanithi if (!oemDiagnosticDataType || !diagnosticDataType) 761a43be80fSAsmitha Karunanithi { 7624978b63fSJason M. Bills BMCWEB_LOG_ERROR 7634978b63fSJason M. Bills << "CreateDump action parameter 'DiagnosticDataType'/'OEMDiagnosticDataType' value not found!"; 764a43be80fSAsmitha Karunanithi messages::actionParameterMissing( 765a43be80fSAsmitha Karunanithi asyncResp->res, "CollectDiagnosticData", 766a43be80fSAsmitha Karunanithi "DiagnosticDataType & OEMDiagnosticDataType"); 767a43be80fSAsmitha Karunanithi return; 768a43be80fSAsmitha Karunanithi } 7693174e4dfSEd Tanous if ((*oemDiagnosticDataType != "System") || 770a43be80fSAsmitha Karunanithi (*diagnosticDataType != "OEM")) 771a43be80fSAsmitha Karunanithi { 772a43be80fSAsmitha Karunanithi BMCWEB_LOG_ERROR << "Wrong parameter values passed"; 773ace85d60SEd Tanous messages::internalError(asyncResp->res); 774a43be80fSAsmitha Karunanithi return; 775a43be80fSAsmitha Karunanithi } 776a43be80fSAsmitha Karunanithi } 777a43be80fSAsmitha Karunanithi else if (dumpType == "BMC") 778a43be80fSAsmitha Karunanithi { 779a43be80fSAsmitha Karunanithi if (!diagnosticDataType) 780a43be80fSAsmitha Karunanithi { 7810fda0f12SGeorge Liu BMCWEB_LOG_ERROR 7820fda0f12SGeorge Liu << "CreateDump action parameter 'DiagnosticDataType' not found!"; 783a43be80fSAsmitha Karunanithi messages::actionParameterMissing( 784a43be80fSAsmitha Karunanithi asyncResp->res, "CollectDiagnosticData", "DiagnosticDataType"); 785a43be80fSAsmitha Karunanithi return; 786a43be80fSAsmitha Karunanithi } 7873174e4dfSEd Tanous if (*diagnosticDataType != "Manager") 788a43be80fSAsmitha Karunanithi { 789a43be80fSAsmitha Karunanithi BMCWEB_LOG_ERROR 790a43be80fSAsmitha Karunanithi << "Wrong parameter value passed for 'DiagnosticDataType'"; 791ace85d60SEd Tanous messages::internalError(asyncResp->res); 792a43be80fSAsmitha Karunanithi return; 793a43be80fSAsmitha Karunanithi } 794a43be80fSAsmitha Karunanithi } 795a43be80fSAsmitha Karunanithi 796a43be80fSAsmitha Karunanithi crow::connections::systemBus->async_method_call( 79798be3e39SEd Tanous [asyncResp, payload(task::Payload(req)), dumpPath, 79898be3e39SEd Tanous dumpType](const boost::system::error_code ec, 79998be3e39SEd Tanous const uint32_t& dumpId) mutable { 800a43be80fSAsmitha Karunanithi if (ec) 801a43be80fSAsmitha Karunanithi { 802a43be80fSAsmitha Karunanithi BMCWEB_LOG_ERROR << "CreateDump resp_handler got error " << ec; 803a43be80fSAsmitha Karunanithi messages::internalError(asyncResp->res); 804a43be80fSAsmitha Karunanithi return; 805a43be80fSAsmitha Karunanithi } 806a43be80fSAsmitha Karunanithi BMCWEB_LOG_DEBUG << "Dump Created. Id: " << dumpId; 807a43be80fSAsmitha Karunanithi 808002d39b4SEd Tanous createDumpTaskCallback(std::move(payload), asyncResp, dumpId, dumpPath, 809002d39b4SEd Tanous dumpType); 810a43be80fSAsmitha Karunanithi }, 811b47452b2SAsmitha Karunanithi "xyz.openbmc_project.Dump.Manager", 812b47452b2SAsmitha Karunanithi "/xyz/openbmc_project/dump/" + 813b47452b2SAsmitha Karunanithi std::string(boost::algorithm::to_lower_copy(dumpType)), 814a43be80fSAsmitha Karunanithi "xyz.openbmc_project.Dump.Create", "CreateDump"); 815a43be80fSAsmitha Karunanithi } 816a43be80fSAsmitha Karunanithi 8178d1b46d7Szhanghch05 inline void clearDump(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp, 8188d1b46d7Szhanghch05 const std::string& dumpType) 81980319af1SAsmitha Karunanithi { 820b47452b2SAsmitha Karunanithi std::string dumpTypeLowerCopy = 821b47452b2SAsmitha Karunanithi std::string(boost::algorithm::to_lower_copy(dumpType)); 8228d1b46d7Szhanghch05 82380319af1SAsmitha Karunanithi crow::connections::systemBus->async_method_call( 824b9d36b47SEd Tanous [asyncResp, dumpType]( 825b9d36b47SEd Tanous const boost::system::error_code ec, 826b9d36b47SEd Tanous const dbus::utility::MapperGetSubTreePathsResponse& subTreePaths) { 82780319af1SAsmitha Karunanithi if (ec) 82880319af1SAsmitha Karunanithi { 82980319af1SAsmitha Karunanithi BMCWEB_LOG_ERROR << "resp_handler got error " << ec; 83080319af1SAsmitha Karunanithi messages::internalError(asyncResp->res); 83180319af1SAsmitha Karunanithi return; 83280319af1SAsmitha Karunanithi } 83380319af1SAsmitha Karunanithi 83480319af1SAsmitha Karunanithi for (const std::string& path : subTreePaths) 83580319af1SAsmitha Karunanithi { 8362dfd18efSEd Tanous sdbusplus::message::object_path objPath(path); 8372dfd18efSEd Tanous std::string logID = objPath.filename(); 8382dfd18efSEd Tanous if (logID.empty()) 83980319af1SAsmitha Karunanithi { 8402dfd18efSEd Tanous continue; 84180319af1SAsmitha Karunanithi } 8422dfd18efSEd Tanous deleteDumpEntry(asyncResp, logID, dumpType); 84380319af1SAsmitha Karunanithi } 84480319af1SAsmitha Karunanithi }, 84580319af1SAsmitha Karunanithi "xyz.openbmc_project.ObjectMapper", 84680319af1SAsmitha Karunanithi "/xyz/openbmc_project/object_mapper", 84780319af1SAsmitha Karunanithi "xyz.openbmc_project.ObjectMapper", "GetSubTreePaths", 848b47452b2SAsmitha Karunanithi "/xyz/openbmc_project/dump/" + dumpTypeLowerCopy, 0, 849b47452b2SAsmitha Karunanithi std::array<std::string, 1>{"xyz.openbmc_project.Dump.Entry." + 850b47452b2SAsmitha Karunanithi dumpType}); 85180319af1SAsmitha Karunanithi } 85280319af1SAsmitha Karunanithi 853b9d36b47SEd Tanous inline static void 854b9d36b47SEd Tanous parseCrashdumpParameters(const dbus::utility::DBusPropertiesMap& params, 855b9d36b47SEd Tanous std::string& filename, std::string& timestamp, 856b9d36b47SEd Tanous std::string& logfile) 857043a0536SJohnathan Mantey { 858043a0536SJohnathan Mantey for (auto property : params) 859043a0536SJohnathan Mantey { 860043a0536SJohnathan Mantey if (property.first == "Timestamp") 861043a0536SJohnathan Mantey { 862043a0536SJohnathan Mantey const std::string* value = 8638d78b7a9SPatrick Williams std::get_if<std::string>(&property.second); 864043a0536SJohnathan Mantey if (value != nullptr) 865043a0536SJohnathan Mantey { 866043a0536SJohnathan Mantey timestamp = *value; 867043a0536SJohnathan Mantey } 868043a0536SJohnathan Mantey } 869043a0536SJohnathan Mantey else if (property.first == "Filename") 870043a0536SJohnathan Mantey { 871043a0536SJohnathan Mantey const std::string* value = 8728d78b7a9SPatrick Williams std::get_if<std::string>(&property.second); 873043a0536SJohnathan Mantey if (value != nullptr) 874043a0536SJohnathan Mantey { 875043a0536SJohnathan Mantey filename = *value; 876043a0536SJohnathan Mantey } 877043a0536SJohnathan Mantey } 878043a0536SJohnathan Mantey else if (property.first == "Log") 879043a0536SJohnathan Mantey { 880043a0536SJohnathan Mantey const std::string* value = 8818d78b7a9SPatrick Williams std::get_if<std::string>(&property.second); 882043a0536SJohnathan Mantey if (value != nullptr) 883043a0536SJohnathan Mantey { 884043a0536SJohnathan Mantey logfile = *value; 885043a0536SJohnathan Mantey } 886043a0536SJohnathan Mantey } 887043a0536SJohnathan Mantey } 888043a0536SJohnathan Mantey } 889043a0536SJohnathan Mantey 890a3316fc6SZhikuiRen constexpr char const* postCodeIface = "xyz.openbmc_project.State.Boot.PostCode"; 8917e860f15SJohn Edward Broadbent inline void requestRoutesSystemLogServiceCollection(App& app) 8921da66f75SEd Tanous { 893c4bf6374SJason M. Bills /** 894c4bf6374SJason M. Bills * Functions triggers appropriate requests on DBus 895c4bf6374SJason M. Bills */ 8967e860f15SJohn Edward Broadbent BMCWEB_ROUTE(app, "/redfish/v1/Systems/system/LogServices/") 897ed398213SEd Tanous .privileges(redfish::privileges::getLogServiceCollection) 898002d39b4SEd Tanous .methods(boost::beast::http::verb::get)( 899002d39b4SEd Tanous [&app](const crow::Request& req, 900002d39b4SEd Tanous const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) { 9013ba00073SCarson Labrado if (!redfish::setUpRedfishRoute(app, req, asyncResp)) 902c4bf6374SJason M. Bills { 90345ca1b86SEd Tanous return; 90445ca1b86SEd Tanous } 9057e860f15SJohn Edward Broadbent // Collections don't include the static data added by SubRoute 9067e860f15SJohn Edward Broadbent // because it has a duplicate entry for members 907c4bf6374SJason M. Bills asyncResp->res.jsonValue["@odata.type"] = 908c4bf6374SJason M. Bills "#LogServiceCollection.LogServiceCollection"; 909c4bf6374SJason M. Bills asyncResp->res.jsonValue["@odata.id"] = 910029573d4SEd Tanous "/redfish/v1/Systems/system/LogServices"; 91145ca1b86SEd Tanous asyncResp->res.jsonValue["Name"] = "System Log Services Collection"; 912c4bf6374SJason M. Bills asyncResp->res.jsonValue["Description"] = 913c4bf6374SJason M. Bills "Collection of LogServices for this Computer System"; 914002d39b4SEd Tanous nlohmann::json& logServiceArray = asyncResp->res.jsonValue["Members"]; 915c4bf6374SJason M. Bills logServiceArray = nlohmann::json::array(); 9161476687dSEd Tanous nlohmann::json::object_t eventLog; 9171476687dSEd Tanous eventLog["@odata.id"] = 9181476687dSEd Tanous "/redfish/v1/Systems/system/LogServices/EventLog"; 9191476687dSEd Tanous logServiceArray.push_back(std::move(eventLog)); 9205cb1dd27SAsmitha Karunanithi #ifdef BMCWEB_ENABLE_REDFISH_DUMP_LOG 9211476687dSEd Tanous nlohmann::json::object_t dumpLog; 922002d39b4SEd Tanous dumpLog["@odata.id"] = "/redfish/v1/Systems/system/LogServices/Dump"; 9231476687dSEd Tanous logServiceArray.push_back(std::move(dumpLog)); 924c9bb6861Sraviteja-b #endif 925c9bb6861Sraviteja-b 926d53dd41fSJason M. Bills #ifdef BMCWEB_ENABLE_REDFISH_CPU_LOG 9271476687dSEd Tanous nlohmann::json::object_t crashdump; 9281476687dSEd Tanous crashdump["@odata.id"] = 9291476687dSEd Tanous "/redfish/v1/Systems/system/LogServices/Crashdump"; 9301476687dSEd Tanous logServiceArray.push_back(std::move(crashdump)); 931d53dd41fSJason M. Bills #endif 932b7028ebfSSpencer Ku 933b7028ebfSSpencer Ku #ifdef BMCWEB_ENABLE_REDFISH_HOST_LOGGER 9341476687dSEd Tanous nlohmann::json::object_t hostlogger; 9351476687dSEd Tanous hostlogger["@odata.id"] = 9361476687dSEd Tanous "/redfish/v1/Systems/system/LogServices/HostLogger"; 9371476687dSEd Tanous logServiceArray.push_back(std::move(hostlogger)); 938b7028ebfSSpencer Ku #endif 939c4bf6374SJason M. Bills asyncResp->res.jsonValue["Members@odata.count"] = 940c4bf6374SJason M. Bills logServiceArray.size(); 941a3316fc6SZhikuiRen 942a3316fc6SZhikuiRen crow::connections::systemBus->async_method_call( 94345ca1b86SEd Tanous [asyncResp](const boost::system::error_code ec, 944b9d36b47SEd Tanous const dbus::utility::MapperGetSubTreePathsResponse& 945b9d36b47SEd Tanous subtreePath) { 946a3316fc6SZhikuiRen if (ec) 947a3316fc6SZhikuiRen { 948a3316fc6SZhikuiRen BMCWEB_LOG_ERROR << ec; 949a3316fc6SZhikuiRen return; 950a3316fc6SZhikuiRen } 951a3316fc6SZhikuiRen 95255f79e6fSEd Tanous for (const auto& pathStr : subtreePath) 953a3316fc6SZhikuiRen { 954a3316fc6SZhikuiRen if (pathStr.find("PostCode") != std::string::npos) 955a3316fc6SZhikuiRen { 95623a21a1cSEd Tanous nlohmann::json& logServiceArrayLocal = 957a3316fc6SZhikuiRen asyncResp->res.jsonValue["Members"]; 95823a21a1cSEd Tanous logServiceArrayLocal.push_back( 9590fda0f12SGeorge Liu {{"@odata.id", 9600fda0f12SGeorge Liu "/redfish/v1/Systems/system/LogServices/PostCodes"}}); 96145ca1b86SEd Tanous asyncResp->res.jsonValue["Members@odata.count"] = 96223a21a1cSEd Tanous logServiceArrayLocal.size(); 963a3316fc6SZhikuiRen return; 964a3316fc6SZhikuiRen } 965a3316fc6SZhikuiRen } 966a3316fc6SZhikuiRen }, 967a3316fc6SZhikuiRen "xyz.openbmc_project.ObjectMapper", 968a3316fc6SZhikuiRen "/xyz/openbmc_project/object_mapper", 96945ca1b86SEd Tanous "xyz.openbmc_project.ObjectMapper", "GetSubTreePaths", "/", 0, 97045ca1b86SEd Tanous std::array<const char*, 1>{postCodeIface}); 9717e860f15SJohn Edward Broadbent }); 972c4bf6374SJason M. Bills } 973c4bf6374SJason M. Bills 9747e860f15SJohn Edward Broadbent inline void requestRoutesEventLogService(App& app) 975c4bf6374SJason M. Bills { 9767e860f15SJohn Edward Broadbent BMCWEB_ROUTE(app, "/redfish/v1/Systems/system/LogServices/EventLog/") 977ed398213SEd Tanous .privileges(redfish::privileges::getLogService) 978002d39b4SEd Tanous .methods(boost::beast::http::verb::get)( 979002d39b4SEd Tanous [&app](const crow::Request& req, 980002d39b4SEd Tanous const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) { 9813ba00073SCarson Labrado if (!redfish::setUpRedfishRoute(app, req, asyncResp)) 98245ca1b86SEd Tanous { 98345ca1b86SEd Tanous return; 98445ca1b86SEd Tanous } 985c4bf6374SJason M. Bills asyncResp->res.jsonValue["@odata.id"] = 986029573d4SEd Tanous "/redfish/v1/Systems/system/LogServices/EventLog"; 987c4bf6374SJason M. Bills asyncResp->res.jsonValue["@odata.type"] = 988c4bf6374SJason M. Bills "#LogService.v1_1_0.LogService"; 989c4bf6374SJason M. Bills asyncResp->res.jsonValue["Name"] = "Event Log Service"; 990002d39b4SEd Tanous asyncResp->res.jsonValue["Description"] = "System Event Log Service"; 991c4bf6374SJason M. Bills asyncResp->res.jsonValue["Id"] = "EventLog"; 992c4bf6374SJason M. Bills asyncResp->res.jsonValue["OverWritePolicy"] = "WrapsWhenFull"; 9937c8c4058STejas Patil 9947c8c4058STejas Patil std::pair<std::string, std::string> redfishDateTimeOffset = 9957c8c4058STejas Patil crow::utility::getDateTimeOffsetNow(); 9967c8c4058STejas Patil 9977c8c4058STejas Patil asyncResp->res.jsonValue["DateTime"] = redfishDateTimeOffset.first; 9987c8c4058STejas Patil asyncResp->res.jsonValue["DateTimeLocalOffset"] = 9997c8c4058STejas Patil redfishDateTimeOffset.second; 10007c8c4058STejas Patil 10011476687dSEd Tanous asyncResp->res.jsonValue["Entries"]["@odata.id"] = 10021476687dSEd Tanous "/redfish/v1/Systems/system/LogServices/EventLog/Entries"; 1003e7d6c8b2SGunnar Mills asyncResp->res.jsonValue["Actions"]["#LogService.ClearLog"] = { 1004e7d6c8b2SGunnar Mills 10050fda0f12SGeorge Liu {"target", 10060fda0f12SGeorge Liu "/redfish/v1/Systems/system/LogServices/EventLog/Actions/LogService.ClearLog"}}; 10077e860f15SJohn Edward Broadbent }); 1008489640c6SJason M. Bills } 1009489640c6SJason M. Bills 10107e860f15SJohn Edward Broadbent inline void requestRoutesJournalEventLogClear(App& app) 1011489640c6SJason M. Bills { 10124978b63fSJason M. Bills BMCWEB_ROUTE( 10134978b63fSJason M. Bills app, 10144978b63fSJason M. Bills "/redfish/v1/Systems/system/LogServices/EventLog/Actions/LogService.ClearLog/") 1015432a890cSEd Tanous .privileges({{"ConfigureComponents"}}) 10167e860f15SJohn Edward Broadbent .methods(boost::beast::http::verb::post)( 101745ca1b86SEd Tanous [&app](const crow::Request& req, 10187e860f15SJohn Edward Broadbent const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) { 10193ba00073SCarson Labrado if (!redfish::setUpRedfishRoute(app, req, asyncResp)) 102045ca1b86SEd Tanous { 102145ca1b86SEd Tanous return; 102245ca1b86SEd Tanous } 1023489640c6SJason M. Bills // Clear the EventLog by deleting the log files 1024489640c6SJason M. Bills std::vector<std::filesystem::path> redfishLogFiles; 1025489640c6SJason M. Bills if (getRedfishLogFiles(redfishLogFiles)) 1026489640c6SJason M. Bills { 1027489640c6SJason M. Bills for (const std::filesystem::path& file : redfishLogFiles) 1028489640c6SJason M. Bills { 1029489640c6SJason M. Bills std::error_code ec; 1030489640c6SJason M. Bills std::filesystem::remove(file, ec); 1031489640c6SJason M. Bills } 1032489640c6SJason M. Bills } 1033489640c6SJason M. Bills 1034489640c6SJason M. Bills // Reload rsyslog so it knows to start new log files 1035489640c6SJason M. Bills crow::connections::systemBus->async_method_call( 1036489640c6SJason M. Bills [asyncResp](const boost::system::error_code ec) { 1037489640c6SJason M. Bills if (ec) 1038489640c6SJason M. Bills { 1039002d39b4SEd Tanous BMCWEB_LOG_ERROR << "Failed to reload rsyslog: " << ec; 1040489640c6SJason M. Bills messages::internalError(asyncResp->res); 1041489640c6SJason M. Bills return; 1042489640c6SJason M. Bills } 1043489640c6SJason M. Bills 1044489640c6SJason M. Bills messages::success(asyncResp->res); 1045489640c6SJason M. Bills }, 1046489640c6SJason M. Bills "org.freedesktop.systemd1", "/org/freedesktop/systemd1", 1047002d39b4SEd Tanous "org.freedesktop.systemd1.Manager", "ReloadUnit", "rsyslog.service", 1048002d39b4SEd Tanous "replace"); 10497e860f15SJohn Edward Broadbent }); 1050c4bf6374SJason M. Bills } 1051c4bf6374SJason M. Bills 1052ac992cdeSJason M. Bills enum class LogParseError 1053ac992cdeSJason M. Bills { 1054ac992cdeSJason M. Bills success, 1055ac992cdeSJason M. Bills parseFailed, 1056ac992cdeSJason M. Bills messageIdNotInRegistry, 1057ac992cdeSJason M. Bills }; 1058ac992cdeSJason M. Bills 1059ac992cdeSJason M. Bills static LogParseError 1060ac992cdeSJason M. Bills fillEventLogEntryJson(const std::string& logEntryID, 1061b5a76932SEd Tanous const std::string& logEntry, 1062de703c5dSJason M. Bills nlohmann::json::object_t& logEntryJson) 1063c4bf6374SJason M. Bills { 106495820184SJason M. Bills // The redfish log format is "<Timestamp> <MessageId>,<MessageArgs>" 1065cd225da8SJason M. Bills // First get the Timestamp 1066f23b7296SEd Tanous size_t space = logEntry.find_first_of(' '); 1067cd225da8SJason M. Bills if (space == std::string::npos) 106895820184SJason M. Bills { 1069ac992cdeSJason M. Bills return LogParseError::parseFailed; 107095820184SJason M. Bills } 1071cd225da8SJason M. Bills std::string timestamp = logEntry.substr(0, space); 1072cd225da8SJason M. Bills // Then get the log contents 1073f23b7296SEd Tanous size_t entryStart = logEntry.find_first_not_of(' ', space); 1074cd225da8SJason M. Bills if (entryStart == std::string::npos) 1075cd225da8SJason M. Bills { 1076ac992cdeSJason M. Bills return LogParseError::parseFailed; 1077cd225da8SJason M. Bills } 1078cd225da8SJason M. Bills std::string_view entry(logEntry); 1079cd225da8SJason M. Bills entry.remove_prefix(entryStart); 1080cd225da8SJason M. Bills // Use split to separate the entry into its fields 1081cd225da8SJason M. Bills std::vector<std::string> logEntryFields; 1082cd225da8SJason M. Bills boost::split(logEntryFields, entry, boost::is_any_of(","), 1083cd225da8SJason M. Bills boost::token_compress_on); 1084cd225da8SJason M. Bills // We need at least a MessageId to be valid 108526f6976fSEd Tanous if (logEntryFields.empty()) 1086cd225da8SJason M. Bills { 1087ac992cdeSJason M. Bills return LogParseError::parseFailed; 1088cd225da8SJason M. Bills } 1089cd225da8SJason M. Bills std::string& messageID = logEntryFields[0]; 109095820184SJason M. Bills 10914851d45dSJason M. Bills // Get the Message from the MessageRegistry 1092fffb8c1fSEd Tanous const registries::Message* message = registries::getMessage(messageID); 1093c4bf6374SJason M. Bills 109454417b02SSui Chen if (message == nullptr) 1095c4bf6374SJason M. Bills { 109654417b02SSui Chen BMCWEB_LOG_WARNING << "Log entry not found in registry: " << logEntry; 1097ac992cdeSJason M. Bills return LogParseError::messageIdNotInRegistry; 1098c4bf6374SJason M. Bills } 1099c4bf6374SJason M. Bills 110054417b02SSui Chen std::string msg = message->message; 110154417b02SSui Chen 110215a86ff6SJason M. Bills // Get the MessageArgs from the log if there are any 110326702d01SEd Tanous std::span<std::string> messageArgs; 110415a86ff6SJason M. Bills if (logEntryFields.size() > 1) 110515a86ff6SJason M. Bills { 110615a86ff6SJason M. Bills std::string& messageArgsStart = logEntryFields[1]; 110715a86ff6SJason M. Bills // If the first string is empty, assume there are no MessageArgs 110815a86ff6SJason M. Bills std::size_t messageArgsSize = 0; 110915a86ff6SJason M. Bills if (!messageArgsStart.empty()) 111015a86ff6SJason M. Bills { 111115a86ff6SJason M. Bills messageArgsSize = logEntryFields.size() - 1; 111215a86ff6SJason M. Bills } 111315a86ff6SJason M. Bills 111423a21a1cSEd Tanous messageArgs = {&messageArgsStart, messageArgsSize}; 1115c4bf6374SJason M. Bills 11164851d45dSJason M. Bills // Fill the MessageArgs into the Message 111795820184SJason M. Bills int i = 0; 111895820184SJason M. Bills for (const std::string& messageArg : messageArgs) 11194851d45dSJason M. Bills { 112095820184SJason M. Bills std::string argStr = "%" + std::to_string(++i); 11214851d45dSJason M. Bills size_t argPos = msg.find(argStr); 11224851d45dSJason M. Bills if (argPos != std::string::npos) 11234851d45dSJason M. Bills { 112495820184SJason M. Bills msg.replace(argPos, argStr.length(), messageArg); 11254851d45dSJason M. Bills } 11264851d45dSJason M. Bills } 112715a86ff6SJason M. Bills } 11284851d45dSJason M. Bills 112995820184SJason M. Bills // Get the Created time from the timestamp. The log timestamp is in RFC3339 113095820184SJason M. Bills // format which matches the Redfish format except for the fractional seconds 113195820184SJason M. Bills // between the '.' and the '+', so just remove them. 1132f23b7296SEd Tanous std::size_t dot = timestamp.find_first_of('.'); 1133f23b7296SEd Tanous std::size_t plus = timestamp.find_first_of('+'); 113495820184SJason M. Bills if (dot != std::string::npos && plus != std::string::npos) 1135c4bf6374SJason M. Bills { 113695820184SJason M. Bills timestamp.erase(dot, plus - dot); 1137c4bf6374SJason M. Bills } 1138c4bf6374SJason M. Bills 1139c4bf6374SJason M. Bills // Fill in the log entry with the gathered data 114084afc48bSJason M. Bills logEntryJson["@odata.type"] = "#LogEntry.v1_8_0.LogEntry"; 114184afc48bSJason M. Bills logEntryJson["@odata.id"] = 114284afc48bSJason M. Bills "/redfish/v1/Systems/system/LogServices/EventLog/Entries/" + logEntryID; 114384afc48bSJason M. Bills logEntryJson["Name"] = "System Event Log Entry"; 114484afc48bSJason M. Bills logEntryJson["Id"] = logEntryID; 114584afc48bSJason M. Bills logEntryJson["Message"] = std::move(msg); 114684afc48bSJason M. Bills logEntryJson["MessageId"] = std::move(messageID); 114784afc48bSJason M. Bills logEntryJson["MessageArgs"] = messageArgs; 114884afc48bSJason M. Bills logEntryJson["EntryType"] = "Event"; 114984afc48bSJason M. Bills logEntryJson["Severity"] = message->messageSeverity; 115084afc48bSJason M. Bills logEntryJson["Created"] = std::move(timestamp); 1151ac992cdeSJason M. Bills return LogParseError::success; 1152c4bf6374SJason M. Bills } 1153c4bf6374SJason M. Bills 11547e860f15SJohn Edward Broadbent inline void requestRoutesJournalEventLogEntryCollection(App& app) 1155c4bf6374SJason M. Bills { 11567e860f15SJohn Edward Broadbent BMCWEB_ROUTE(app, 11577e860f15SJohn Edward Broadbent "/redfish/v1/Systems/system/LogServices/EventLog/Entries/") 11588b6a35f0SGunnar Mills .privileges(redfish::privileges::getLogEntryCollection) 1159002d39b4SEd Tanous .methods(boost::beast::http::verb::get)( 1160002d39b4SEd Tanous [&app](const crow::Request& req, 1161002d39b4SEd Tanous const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) { 1162c937d2bfSEd Tanous query_param::QueryCapabilities capabilities = { 1163c937d2bfSEd Tanous .canDelegateTop = true, 1164c937d2bfSEd Tanous .canDelegateSkip = true, 1165c937d2bfSEd Tanous }; 1166c937d2bfSEd Tanous query_param::Query delegatedQuery; 1167c937d2bfSEd Tanous if (!redfish::setUpRedfishRouteWithDelegation( 11683ba00073SCarson Labrado app, req, asyncResp, delegatedQuery, capabilities)) 1169c4bf6374SJason M. Bills { 1170c4bf6374SJason M. Bills return; 1171c4bf6374SJason M. Bills } 11727e860f15SJohn Edward Broadbent // Collections don't include the static data added by SubRoute 11737e860f15SJohn Edward Broadbent // because it has a duplicate entry for members 1174c4bf6374SJason M. Bills asyncResp->res.jsonValue["@odata.type"] = 1175c4bf6374SJason M. Bills "#LogEntryCollection.LogEntryCollection"; 1176c4bf6374SJason M. Bills asyncResp->res.jsonValue["@odata.id"] = 1177029573d4SEd Tanous "/redfish/v1/Systems/system/LogServices/EventLog/Entries"; 1178c4bf6374SJason M. Bills asyncResp->res.jsonValue["Name"] = "System Event Log Entries"; 1179c4bf6374SJason M. Bills asyncResp->res.jsonValue["Description"] = 1180c4bf6374SJason M. Bills "Collection of System Event Log Entries"; 1181cb92c03bSAndrew Geissler 11824978b63fSJason M. Bills nlohmann::json& logEntryArray = asyncResp->res.jsonValue["Members"]; 1183c4bf6374SJason M. Bills logEntryArray = nlohmann::json::array(); 11847e860f15SJohn Edward Broadbent // Go through the log files and create a unique ID for each 11857e860f15SJohn Edward Broadbent // entry 118695820184SJason M. Bills std::vector<std::filesystem::path> redfishLogFiles; 118795820184SJason M. Bills getRedfishLogFiles(redfishLogFiles); 1188b01bf299SEd Tanous uint64_t entryCount = 0; 1189cd225da8SJason M. Bills std::string logEntry; 119095820184SJason M. Bills 11917e860f15SJohn Edward Broadbent // Oldest logs are in the last file, so start there and loop 11927e860f15SJohn Edward Broadbent // backwards 1193002d39b4SEd Tanous for (auto it = redfishLogFiles.rbegin(); it < redfishLogFiles.rend(); 1194002d39b4SEd Tanous it++) 1195c4bf6374SJason M. Bills { 1196cd225da8SJason M. Bills std::ifstream logStream(*it); 119795820184SJason M. Bills if (!logStream.is_open()) 1198c4bf6374SJason M. Bills { 1199c4bf6374SJason M. Bills continue; 1200c4bf6374SJason M. Bills } 1201c4bf6374SJason M. Bills 1202e85d6b16SJason M. Bills // Reset the unique ID on the first entry 1203e85d6b16SJason M. Bills bool firstEntry = true; 120495820184SJason M. Bills while (std::getline(logStream, logEntry)) 120595820184SJason M. Bills { 1206c4bf6374SJason M. Bills std::string idStr; 1207e85d6b16SJason M. Bills if (!getUniqueEntryID(logEntry, idStr, firstEntry)) 1208c4bf6374SJason M. Bills { 1209c4bf6374SJason M. Bills continue; 1210c4bf6374SJason M. Bills } 1211e85d6b16SJason M. Bills firstEntry = false; 1212e85d6b16SJason M. Bills 1213de703c5dSJason M. Bills nlohmann::json::object_t bmcLogEntry; 1214ac992cdeSJason M. Bills LogParseError status = 1215ac992cdeSJason M. Bills fillEventLogEntryJson(idStr, logEntry, bmcLogEntry); 1216ac992cdeSJason M. Bills if (status == LogParseError::messageIdNotInRegistry) 1217ac992cdeSJason M. Bills { 1218ac992cdeSJason M. Bills continue; 1219ac992cdeSJason M. Bills } 1220ac992cdeSJason M. Bills if (status != LogParseError::success) 1221c4bf6374SJason M. Bills { 1222c4bf6374SJason M. Bills messages::internalError(asyncResp->res); 1223c4bf6374SJason M. Bills return; 1224c4bf6374SJason M. Bills } 1225de703c5dSJason M. Bills 1226de703c5dSJason M. Bills entryCount++; 1227de703c5dSJason M. Bills // Handle paging using skip (number of entries to skip from the 1228de703c5dSJason M. Bills // start) and top (number of entries to display) 1229de703c5dSJason M. Bills if (entryCount <= delegatedQuery.skip || 1230de703c5dSJason M. Bills entryCount > delegatedQuery.skip + delegatedQuery.top) 1231de703c5dSJason M. Bills { 1232de703c5dSJason M. Bills continue; 1233de703c5dSJason M. Bills } 1234de703c5dSJason M. Bills 1235de703c5dSJason M. Bills logEntryArray.push_back(std::move(bmcLogEntry)); 1236c4bf6374SJason M. Bills } 123795820184SJason M. Bills } 1238c4bf6374SJason M. Bills asyncResp->res.jsonValue["Members@odata.count"] = entryCount; 1239c937d2bfSEd Tanous if (delegatedQuery.skip + delegatedQuery.top < entryCount) 1240c4bf6374SJason M. Bills { 1241c4bf6374SJason M. Bills asyncResp->res.jsonValue["Members@odata.nextLink"] = 12424978b63fSJason M. Bills "/redfish/v1/Systems/system/LogServices/EventLog/Entries?$skip=" + 1243c937d2bfSEd Tanous std::to_string(delegatedQuery.skip + delegatedQuery.top); 1244c4bf6374SJason M. Bills } 12457e860f15SJohn Edward Broadbent }); 1246897967deSJason M. Bills } 1247897967deSJason M. Bills 12487e860f15SJohn Edward Broadbent inline void requestRoutesJournalEventLogEntry(App& app) 1249897967deSJason M. Bills { 12507e860f15SJohn Edward Broadbent BMCWEB_ROUTE( 12517e860f15SJohn Edward Broadbent app, "/redfish/v1/Systems/system/LogServices/EventLog/Entries/<str>/") 1252ed398213SEd Tanous .privileges(redfish::privileges::getLogEntry) 12537e860f15SJohn Edward Broadbent .methods(boost::beast::http::verb::get)( 125445ca1b86SEd Tanous [&app](const crow::Request& req, 12557e860f15SJohn Edward Broadbent const std::shared_ptr<bmcweb::AsyncResp>& asyncResp, 12567e860f15SJohn Edward Broadbent const std::string& param) { 12573ba00073SCarson Labrado if (!redfish::setUpRedfishRoute(app, req, asyncResp)) 125845ca1b86SEd Tanous { 125945ca1b86SEd Tanous return; 126045ca1b86SEd Tanous } 12617e860f15SJohn Edward Broadbent const std::string& targetID = param; 12628d1b46d7Szhanghch05 12637e860f15SJohn Edward Broadbent // Go through the log files and check the unique ID for each 12647e860f15SJohn Edward Broadbent // entry to find the target entry 1265897967deSJason M. Bills std::vector<std::filesystem::path> redfishLogFiles; 1266897967deSJason M. Bills getRedfishLogFiles(redfishLogFiles); 1267897967deSJason M. Bills std::string logEntry; 1268897967deSJason M. Bills 12697e860f15SJohn Edward Broadbent // Oldest logs are in the last file, so start there and loop 12707e860f15SJohn Edward Broadbent // backwards 1271002d39b4SEd Tanous for (auto it = redfishLogFiles.rbegin(); it < redfishLogFiles.rend(); 1272002d39b4SEd Tanous it++) 1273897967deSJason M. Bills { 1274897967deSJason M. Bills std::ifstream logStream(*it); 1275897967deSJason M. Bills if (!logStream.is_open()) 1276897967deSJason M. Bills { 1277897967deSJason M. Bills continue; 1278897967deSJason M. Bills } 1279897967deSJason M. Bills 1280897967deSJason M. Bills // Reset the unique ID on the first entry 1281897967deSJason M. Bills bool firstEntry = true; 1282897967deSJason M. Bills while (std::getline(logStream, logEntry)) 1283897967deSJason M. Bills { 1284897967deSJason M. Bills std::string idStr; 1285897967deSJason M. Bills if (!getUniqueEntryID(logEntry, idStr, firstEntry)) 1286897967deSJason M. Bills { 1287897967deSJason M. Bills continue; 1288897967deSJason M. Bills } 1289897967deSJason M. Bills firstEntry = false; 1290897967deSJason M. Bills 1291897967deSJason M. Bills if (idStr == targetID) 1292897967deSJason M. Bills { 1293de703c5dSJason M. Bills nlohmann::json::object_t bmcLogEntry; 1294ac992cdeSJason M. Bills LogParseError status = 1295ac992cdeSJason M. Bills fillEventLogEntryJson(idStr, logEntry, bmcLogEntry); 1296ac992cdeSJason M. Bills if (status != LogParseError::success) 1297897967deSJason M. Bills { 1298897967deSJason M. Bills messages::internalError(asyncResp->res); 1299897967deSJason M. Bills return; 1300897967deSJason M. Bills } 1301d405bb51SJason M. Bills asyncResp->res.jsonValue.update(bmcLogEntry); 1302897967deSJason M. Bills return; 1303897967deSJason M. Bills } 1304897967deSJason M. Bills } 1305897967deSJason M. Bills } 1306897967deSJason M. Bills // Requested ID was not found 1307002d39b4SEd Tanous messages::resourceMissingAtURI(asyncResp->res, 1308002d39b4SEd Tanous crow::utility::urlFromPieces(targetID)); 13097e860f15SJohn Edward Broadbent }); 131008a4e4b5SAnthony Wilson } 131108a4e4b5SAnthony Wilson 13127e860f15SJohn Edward Broadbent inline void requestRoutesDBusEventLogEntryCollection(App& app) 131308a4e4b5SAnthony Wilson { 13147e860f15SJohn Edward Broadbent BMCWEB_ROUTE(app, 13157e860f15SJohn Edward Broadbent "/redfish/v1/Systems/system/LogServices/EventLog/Entries/") 1316ed398213SEd Tanous .privileges(redfish::privileges::getLogEntryCollection) 1317002d39b4SEd Tanous .methods(boost::beast::http::verb::get)( 1318002d39b4SEd Tanous [&app](const crow::Request& req, 1319002d39b4SEd Tanous const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) { 13203ba00073SCarson Labrado if (!redfish::setUpRedfishRoute(app, req, asyncResp)) 132145ca1b86SEd Tanous { 132245ca1b86SEd Tanous return; 132345ca1b86SEd Tanous } 13247e860f15SJohn Edward Broadbent // Collections don't include the static data added by SubRoute 13257e860f15SJohn Edward Broadbent // because it has a duplicate entry for members 132608a4e4b5SAnthony Wilson asyncResp->res.jsonValue["@odata.type"] = 132708a4e4b5SAnthony Wilson "#LogEntryCollection.LogEntryCollection"; 132808a4e4b5SAnthony Wilson asyncResp->res.jsonValue["@odata.id"] = 132908a4e4b5SAnthony Wilson "/redfish/v1/Systems/system/LogServices/EventLog/Entries"; 133008a4e4b5SAnthony Wilson asyncResp->res.jsonValue["Name"] = "System Event Log Entries"; 133108a4e4b5SAnthony Wilson asyncResp->res.jsonValue["Description"] = 133208a4e4b5SAnthony Wilson "Collection of System Event Log Entries"; 133308a4e4b5SAnthony Wilson 1334cb92c03bSAndrew Geissler // DBus implementation of EventLog/Entries 1335cb92c03bSAndrew Geissler // Make call to Logging Service to find all log entry objects 1336cb92c03bSAndrew Geissler crow::connections::systemBus->async_method_call( 1337cb92c03bSAndrew Geissler [asyncResp](const boost::system::error_code ec, 1338914e2d5dSEd Tanous const dbus::utility::ManagedObjectType& resp) { 1339cb92c03bSAndrew Geissler if (ec) 1340cb92c03bSAndrew Geissler { 1341cb92c03bSAndrew Geissler // TODO Handle for specific error code 1342cb92c03bSAndrew Geissler BMCWEB_LOG_ERROR 1343002d39b4SEd Tanous << "getLogEntriesIfaceData resp_handler got error " << ec; 1344cb92c03bSAndrew Geissler messages::internalError(asyncResp->res); 1345cb92c03bSAndrew Geissler return; 1346cb92c03bSAndrew Geissler } 1347002d39b4SEd Tanous nlohmann::json& entriesArray = asyncResp->res.jsonValue["Members"]; 1348cb92c03bSAndrew Geissler entriesArray = nlohmann::json::array(); 13499eb808c1SEd Tanous for (const auto& objectPath : resp) 1350cb92c03bSAndrew Geissler { 1351914e2d5dSEd Tanous const uint32_t* id = nullptr; 1352c419c759SEd Tanous const uint64_t* timestamp = nullptr; 1353c419c759SEd Tanous const uint64_t* updateTimestamp = nullptr; 1354914e2d5dSEd Tanous const std::string* severity = nullptr; 1355914e2d5dSEd Tanous const std::string* message = nullptr; 1356914e2d5dSEd Tanous const std::string* filePath = nullptr; 135775710de2SXiaochao Ma bool resolved = false; 13589eb808c1SEd Tanous for (const auto& interfaceMap : objectPath.second) 1359f86bb901SAdriana Kobylak { 1360f86bb901SAdriana Kobylak if (interfaceMap.first == 1361f86bb901SAdriana Kobylak "xyz.openbmc_project.Logging.Entry") 1362f86bb901SAdriana Kobylak { 1363002d39b4SEd Tanous for (const auto& propertyMap : interfaceMap.second) 1364cb92c03bSAndrew Geissler { 1365cb92c03bSAndrew Geissler if (propertyMap.first == "Id") 1366cb92c03bSAndrew Geissler { 1367002d39b4SEd Tanous id = std::get_if<uint32_t>(&propertyMap.second); 1368cb92c03bSAndrew Geissler } 1369cb92c03bSAndrew Geissler else if (propertyMap.first == "Timestamp") 1370cb92c03bSAndrew Geissler { 1371002d39b4SEd Tanous timestamp = 1372002d39b4SEd Tanous std::get_if<uint64_t>(&propertyMap.second); 13737e860f15SJohn Edward Broadbent } 1374002d39b4SEd Tanous else if (propertyMap.first == "UpdateTimestamp") 13757e860f15SJohn Edward Broadbent { 1376002d39b4SEd Tanous updateTimestamp = 1377002d39b4SEd Tanous std::get_if<uint64_t>(&propertyMap.second); 13787e860f15SJohn Edward Broadbent } 13797e860f15SJohn Edward Broadbent else if (propertyMap.first == "Severity") 13807e860f15SJohn Edward Broadbent { 13817e860f15SJohn Edward Broadbent severity = std::get_if<std::string>( 13827e860f15SJohn Edward Broadbent &propertyMap.second); 13837e860f15SJohn Edward Broadbent } 13847e860f15SJohn Edward Broadbent else if (propertyMap.first == "Message") 13857e860f15SJohn Edward Broadbent { 13867e860f15SJohn Edward Broadbent message = std::get_if<std::string>( 13877e860f15SJohn Edward Broadbent &propertyMap.second); 13887e860f15SJohn Edward Broadbent } 13897e860f15SJohn Edward Broadbent else if (propertyMap.first == "Resolved") 13907e860f15SJohn Edward Broadbent { 1391914e2d5dSEd Tanous const bool* resolveptr = 1392002d39b4SEd Tanous std::get_if<bool>(&propertyMap.second); 13937e860f15SJohn Edward Broadbent if (resolveptr == nullptr) 13947e860f15SJohn Edward Broadbent { 1395002d39b4SEd Tanous messages::internalError(asyncResp->res); 13967e860f15SJohn Edward Broadbent return; 13977e860f15SJohn Edward Broadbent } 13987e860f15SJohn Edward Broadbent resolved = *resolveptr; 13997e860f15SJohn Edward Broadbent } 14007e860f15SJohn Edward Broadbent } 14017e860f15SJohn Edward Broadbent if (id == nullptr || message == nullptr || 14027e860f15SJohn Edward Broadbent severity == nullptr) 14037e860f15SJohn Edward Broadbent { 14047e860f15SJohn Edward Broadbent messages::internalError(asyncResp->res); 14057e860f15SJohn Edward Broadbent return; 14067e860f15SJohn Edward Broadbent } 14077e860f15SJohn Edward Broadbent } 14087e860f15SJohn Edward Broadbent else if (interfaceMap.first == 14097e860f15SJohn Edward Broadbent "xyz.openbmc_project.Common.FilePath") 14107e860f15SJohn Edward Broadbent { 1411002d39b4SEd Tanous for (const auto& propertyMap : interfaceMap.second) 14127e860f15SJohn Edward Broadbent { 14137e860f15SJohn Edward Broadbent if (propertyMap.first == "Path") 14147e860f15SJohn Edward Broadbent { 14157e860f15SJohn Edward Broadbent filePath = std::get_if<std::string>( 14167e860f15SJohn Edward Broadbent &propertyMap.second); 14177e860f15SJohn Edward Broadbent } 14187e860f15SJohn Edward Broadbent } 14197e860f15SJohn Edward Broadbent } 14207e860f15SJohn Edward Broadbent } 14217e860f15SJohn Edward Broadbent // Object path without the 14227e860f15SJohn Edward Broadbent // xyz.openbmc_project.Logging.Entry interface, ignore 14237e860f15SJohn Edward Broadbent // and continue. 14247e860f15SJohn Edward Broadbent if (id == nullptr || message == nullptr || 1425c419c759SEd Tanous severity == nullptr || timestamp == nullptr || 1426c419c759SEd Tanous updateTimestamp == nullptr) 14277e860f15SJohn Edward Broadbent { 14287e860f15SJohn Edward Broadbent continue; 14297e860f15SJohn Edward Broadbent } 14307e860f15SJohn Edward Broadbent entriesArray.push_back({}); 14317e860f15SJohn Edward Broadbent nlohmann::json& thisEntry = entriesArray.back(); 14327e860f15SJohn Edward Broadbent thisEntry["@odata.type"] = "#LogEntry.v1_8_0.LogEntry"; 14337e860f15SJohn Edward Broadbent thisEntry["@odata.id"] = 14340fda0f12SGeorge Liu "/redfish/v1/Systems/system/LogServices/EventLog/Entries/" + 14357e860f15SJohn Edward Broadbent std::to_string(*id); 14367e860f15SJohn Edward Broadbent thisEntry["Name"] = "System Event Log Entry"; 14377e860f15SJohn Edward Broadbent thisEntry["Id"] = std::to_string(*id); 14387e860f15SJohn Edward Broadbent thisEntry["Message"] = *message; 14397e860f15SJohn Edward Broadbent thisEntry["Resolved"] = resolved; 14407e860f15SJohn Edward Broadbent thisEntry["EntryType"] = "Event"; 14417e860f15SJohn Edward Broadbent thisEntry["Severity"] = 14427e860f15SJohn Edward Broadbent translateSeverityDbusToRedfish(*severity); 14437e860f15SJohn Edward Broadbent thisEntry["Created"] = 1444c419c759SEd Tanous crow::utility::getDateTimeUintMs(*timestamp); 14457e860f15SJohn Edward Broadbent thisEntry["Modified"] = 1446c419c759SEd Tanous crow::utility::getDateTimeUintMs(*updateTimestamp); 14477e860f15SJohn Edward Broadbent if (filePath != nullptr) 14487e860f15SJohn Edward Broadbent { 14497e860f15SJohn Edward Broadbent thisEntry["AdditionalDataURI"] = 14500fda0f12SGeorge Liu "/redfish/v1/Systems/system/LogServices/EventLog/Entries/" + 14517e860f15SJohn Edward Broadbent std::to_string(*id) + "/attachment"; 14527e860f15SJohn Edward Broadbent } 14537e860f15SJohn Edward Broadbent } 1454002d39b4SEd Tanous std::sort( 1455002d39b4SEd Tanous entriesArray.begin(), entriesArray.end(), 1456002d39b4SEd Tanous [](const nlohmann::json& left, const nlohmann::json& right) { 14577e860f15SJohn Edward Broadbent return (left["Id"] <= right["Id"]); 14587e860f15SJohn Edward Broadbent }); 14597e860f15SJohn Edward Broadbent asyncResp->res.jsonValue["Members@odata.count"] = 14607e860f15SJohn Edward Broadbent entriesArray.size(); 14617e860f15SJohn Edward Broadbent }, 14627e860f15SJohn Edward Broadbent "xyz.openbmc_project.Logging", "/xyz/openbmc_project/logging", 14637e860f15SJohn Edward Broadbent "org.freedesktop.DBus.ObjectManager", "GetManagedObjects"); 14647e860f15SJohn Edward Broadbent }); 14657e860f15SJohn Edward Broadbent } 14667e860f15SJohn Edward Broadbent 14677e860f15SJohn Edward Broadbent inline void requestRoutesDBusEventLogEntry(App& app) 14687e860f15SJohn Edward Broadbent { 14697e860f15SJohn Edward Broadbent BMCWEB_ROUTE( 14707e860f15SJohn Edward Broadbent app, "/redfish/v1/Systems/system/LogServices/EventLog/Entries/<str>/") 1471ed398213SEd Tanous .privileges(redfish::privileges::getLogEntry) 1472002d39b4SEd Tanous .methods(boost::beast::http::verb::get)( 1473002d39b4SEd Tanous [&app](const crow::Request& req, 14747e860f15SJohn Edward Broadbent const std::shared_ptr<bmcweb::AsyncResp>& asyncResp, 147545ca1b86SEd Tanous const std::string& param) { 14763ba00073SCarson Labrado if (!redfish::setUpRedfishRoute(app, req, asyncResp)) 14777e860f15SJohn Edward Broadbent { 147845ca1b86SEd Tanous return; 147945ca1b86SEd Tanous } 14807e860f15SJohn Edward Broadbent std::string entryID = param; 14817e860f15SJohn Edward Broadbent dbus::utility::escapePathForDbus(entryID); 14827e860f15SJohn Edward Broadbent 14837e860f15SJohn Edward Broadbent // DBus implementation of EventLog/Entries 14847e860f15SJohn Edward Broadbent // Make call to Logging Service to find all log entry objects 14857e860f15SJohn Edward Broadbent crow::connections::systemBus->async_method_call( 1486002d39b4SEd Tanous [asyncResp, entryID](const boost::system::error_code ec, 1487b9d36b47SEd Tanous const dbus::utility::DBusPropertiesMap& resp) { 14887e860f15SJohn Edward Broadbent if (ec.value() == EBADR) 14897e860f15SJohn Edward Broadbent { 1490002d39b4SEd Tanous messages::resourceNotFound(asyncResp->res, "EventLogEntry", 1491002d39b4SEd Tanous entryID); 14927e860f15SJohn Edward Broadbent return; 14937e860f15SJohn Edward Broadbent } 14947e860f15SJohn Edward Broadbent if (ec) 14957e860f15SJohn Edward Broadbent { 14960fda0f12SGeorge Liu BMCWEB_LOG_ERROR 1497002d39b4SEd Tanous << "EventLogEntry (DBus) resp_handler got error " << ec; 14987e860f15SJohn Edward Broadbent messages::internalError(asyncResp->res); 14997e860f15SJohn Edward Broadbent return; 15007e860f15SJohn Edward Broadbent } 1501914e2d5dSEd Tanous const uint32_t* id = nullptr; 1502c419c759SEd Tanous const uint64_t* timestamp = nullptr; 1503c419c759SEd Tanous const uint64_t* updateTimestamp = nullptr; 1504914e2d5dSEd Tanous const std::string* severity = nullptr; 1505914e2d5dSEd Tanous const std::string* message = nullptr; 1506914e2d5dSEd Tanous const std::string* filePath = nullptr; 15077e860f15SJohn Edward Broadbent bool resolved = false; 15087e860f15SJohn Edward Broadbent 15099eb808c1SEd Tanous for (const auto& propertyMap : resp) 15107e860f15SJohn Edward Broadbent { 15117e860f15SJohn Edward Broadbent if (propertyMap.first == "Id") 15127e860f15SJohn Edward Broadbent { 15137e860f15SJohn Edward Broadbent id = std::get_if<uint32_t>(&propertyMap.second); 15147e860f15SJohn Edward Broadbent } 15157e860f15SJohn Edward Broadbent else if (propertyMap.first == "Timestamp") 15167e860f15SJohn Edward Broadbent { 1517002d39b4SEd Tanous timestamp = std::get_if<uint64_t>(&propertyMap.second); 1518ebd45906SGeorge Liu } 1519d139c236SGeorge Liu else if (propertyMap.first == "UpdateTimestamp") 1520d139c236SGeorge Liu { 1521ebd45906SGeorge Liu updateTimestamp = 1522c419c759SEd Tanous std::get_if<uint64_t>(&propertyMap.second); 1523ebd45906SGeorge Liu } 1524cb92c03bSAndrew Geissler else if (propertyMap.first == "Severity") 1525cb92c03bSAndrew Geissler { 1526002d39b4SEd Tanous severity = std::get_if<std::string>(&propertyMap.second); 1527cb92c03bSAndrew Geissler } 1528cb92c03bSAndrew Geissler else if (propertyMap.first == "Message") 1529cb92c03bSAndrew Geissler { 1530002d39b4SEd Tanous message = std::get_if<std::string>(&propertyMap.second); 1531ae34c8e8SAdriana Kobylak } 153275710de2SXiaochao Ma else if (propertyMap.first == "Resolved") 153375710de2SXiaochao Ma { 1534914e2d5dSEd Tanous const bool* resolveptr = 153575710de2SXiaochao Ma std::get_if<bool>(&propertyMap.second); 153675710de2SXiaochao Ma if (resolveptr == nullptr) 153775710de2SXiaochao Ma { 153875710de2SXiaochao Ma messages::internalError(asyncResp->res); 153975710de2SXiaochao Ma return; 154075710de2SXiaochao Ma } 154175710de2SXiaochao Ma resolved = *resolveptr; 154275710de2SXiaochao Ma } 15437e860f15SJohn Edward Broadbent else if (propertyMap.first == "Path") 1544f86bb901SAdriana Kobylak { 1545002d39b4SEd Tanous filePath = std::get_if<std::string>(&propertyMap.second); 1546f86bb901SAdriana Kobylak } 1547f86bb901SAdriana Kobylak } 1548002d39b4SEd Tanous if (id == nullptr || message == nullptr || severity == nullptr || 1549002d39b4SEd Tanous timestamp == nullptr || updateTimestamp == nullptr) 1550f86bb901SAdriana Kobylak { 1551ae34c8e8SAdriana Kobylak messages::internalError(asyncResp->res); 1552271584abSEd Tanous return; 1553271584abSEd Tanous } 1554f86bb901SAdriana Kobylak asyncResp->res.jsonValue["@odata.type"] = 1555f86bb901SAdriana Kobylak "#LogEntry.v1_8_0.LogEntry"; 1556f86bb901SAdriana Kobylak asyncResp->res.jsonValue["@odata.id"] = 15570fda0f12SGeorge Liu "/redfish/v1/Systems/system/LogServices/EventLog/Entries/" + 1558f86bb901SAdriana Kobylak std::to_string(*id); 155945ca1b86SEd Tanous asyncResp->res.jsonValue["Name"] = "System Event Log Entry"; 1560f86bb901SAdriana Kobylak asyncResp->res.jsonValue["Id"] = std::to_string(*id); 1561f86bb901SAdriana Kobylak asyncResp->res.jsonValue["Message"] = *message; 1562f86bb901SAdriana Kobylak asyncResp->res.jsonValue["Resolved"] = resolved; 1563f86bb901SAdriana Kobylak asyncResp->res.jsonValue["EntryType"] = "Event"; 1564f86bb901SAdriana Kobylak asyncResp->res.jsonValue["Severity"] = 1565f86bb901SAdriana Kobylak translateSeverityDbusToRedfish(*severity); 1566f86bb901SAdriana Kobylak asyncResp->res.jsonValue["Created"] = 1567c419c759SEd Tanous crow::utility::getDateTimeUintMs(*timestamp); 1568f86bb901SAdriana Kobylak asyncResp->res.jsonValue["Modified"] = 1569c419c759SEd Tanous crow::utility::getDateTimeUintMs(*updateTimestamp); 1570f86bb901SAdriana Kobylak if (filePath != nullptr) 1571f86bb901SAdriana Kobylak { 1572f86bb901SAdriana Kobylak asyncResp->res.jsonValue["AdditionalDataURI"] = 1573e7dbd530SPotin Lai "/redfish/v1/Systems/system/LogServices/EventLog/Entries/" + 1574e7dbd530SPotin Lai std::to_string(*id) + "/attachment"; 1575f86bb901SAdriana Kobylak } 1576cb92c03bSAndrew Geissler }, 1577cb92c03bSAndrew Geissler "xyz.openbmc_project.Logging", 1578cb92c03bSAndrew Geissler "/xyz/openbmc_project/logging/entry/" + entryID, 1579f86bb901SAdriana Kobylak "org.freedesktop.DBus.Properties", "GetAll", ""); 15807e860f15SJohn Edward Broadbent }); 1581336e96c6SChicago Duan 15827e860f15SJohn Edward Broadbent BMCWEB_ROUTE( 15837e860f15SJohn Edward Broadbent app, "/redfish/v1/Systems/system/LogServices/EventLog/Entries/<str>/") 1584ed398213SEd Tanous .privileges(redfish::privileges::patchLogEntry) 15857e860f15SJohn Edward Broadbent .methods(boost::beast::http::verb::patch)( 158645ca1b86SEd Tanous [&app](const crow::Request& req, 15877e860f15SJohn Edward Broadbent const std::shared_ptr<bmcweb::AsyncResp>& asyncResp, 15887e860f15SJohn Edward Broadbent const std::string& entryId) { 15893ba00073SCarson Labrado if (!redfish::setUpRedfishRoute(app, req, asyncResp)) 159045ca1b86SEd Tanous { 159145ca1b86SEd Tanous return; 159245ca1b86SEd Tanous } 159375710de2SXiaochao Ma std::optional<bool> resolved; 159475710de2SXiaochao Ma 159515ed6780SWilly Tu if (!json_util::readJsonPatch(req, asyncResp->res, "Resolved", 15967e860f15SJohn Edward Broadbent resolved)) 159775710de2SXiaochao Ma { 159875710de2SXiaochao Ma return; 159975710de2SXiaochao Ma } 160075710de2SXiaochao Ma BMCWEB_LOG_DEBUG << "Set Resolved"; 160175710de2SXiaochao Ma 160275710de2SXiaochao Ma crow::connections::systemBus->async_method_call( 16034f48d5f6SEd Tanous [asyncResp, entryId](const boost::system::error_code ec) { 160475710de2SXiaochao Ma if (ec) 160575710de2SXiaochao Ma { 160675710de2SXiaochao Ma BMCWEB_LOG_DEBUG << "DBUS response error " << ec; 160775710de2SXiaochao Ma messages::internalError(asyncResp->res); 160875710de2SXiaochao Ma return; 160975710de2SXiaochao Ma } 161075710de2SXiaochao Ma }, 161175710de2SXiaochao Ma "xyz.openbmc_project.Logging", 161275710de2SXiaochao Ma "/xyz/openbmc_project/logging/entry/" + entryId, 161375710de2SXiaochao Ma "org.freedesktop.DBus.Properties", "Set", 161475710de2SXiaochao Ma "xyz.openbmc_project.Logging.Entry", "Resolved", 1615168e20c1SEd Tanous dbus::utility::DbusVariantType(*resolved)); 16167e860f15SJohn Edward Broadbent }); 161775710de2SXiaochao Ma 16187e860f15SJohn Edward Broadbent BMCWEB_ROUTE( 16197e860f15SJohn Edward Broadbent app, "/redfish/v1/Systems/system/LogServices/EventLog/Entries/<str>/") 1620ed398213SEd Tanous .privileges(redfish::privileges::deleteLogEntry) 1621ed398213SEd Tanous 1622002d39b4SEd Tanous .methods(boost::beast::http::verb::delete_)( 1623002d39b4SEd Tanous [&app](const crow::Request& req, 1624002d39b4SEd Tanous const std::shared_ptr<bmcweb::AsyncResp>& asyncResp, 162545ca1b86SEd Tanous const std::string& param) { 16263ba00073SCarson Labrado if (!redfish::setUpRedfishRoute(app, req, asyncResp)) 1627336e96c6SChicago Duan { 162845ca1b86SEd Tanous return; 162945ca1b86SEd Tanous } 1630336e96c6SChicago Duan BMCWEB_LOG_DEBUG << "Do delete single event entries."; 1631336e96c6SChicago Duan 16327e860f15SJohn Edward Broadbent std::string entryID = param; 1633336e96c6SChicago Duan 1634336e96c6SChicago Duan dbus::utility::escapePathForDbus(entryID); 1635336e96c6SChicago Duan 1636336e96c6SChicago Duan // Process response from Logging service. 1637002d39b4SEd Tanous auto respHandler = 1638002d39b4SEd Tanous [asyncResp, entryID](const boost::system::error_code ec) { 1639002d39b4SEd Tanous BMCWEB_LOG_DEBUG << "EventLogEntry (DBus) doDelete callback: Done"; 1640336e96c6SChicago Duan if (ec) 1641336e96c6SChicago Duan { 16423de8d8baSGeorge Liu if (ec.value() == EBADR) 16433de8d8baSGeorge Liu { 164445ca1b86SEd Tanous messages::resourceNotFound(asyncResp->res, "LogEntry", 164545ca1b86SEd Tanous entryID); 16463de8d8baSGeorge Liu return; 16473de8d8baSGeorge Liu } 1648336e96c6SChicago Duan // TODO Handle for specific error code 16490fda0f12SGeorge Liu BMCWEB_LOG_ERROR 16500fda0f12SGeorge Liu << "EventLogEntry (DBus) doDelete respHandler got error " 1651336e96c6SChicago Duan << ec; 1652336e96c6SChicago Duan asyncResp->res.result( 1653336e96c6SChicago Duan boost::beast::http::status::internal_server_error); 1654336e96c6SChicago Duan return; 1655336e96c6SChicago Duan } 1656336e96c6SChicago Duan 1657336e96c6SChicago Duan asyncResp->res.result(boost::beast::http::status::ok); 1658336e96c6SChicago Duan }; 1659336e96c6SChicago Duan 1660336e96c6SChicago Duan // Make call to Logging service to request Delete Log 1661336e96c6SChicago Duan crow::connections::systemBus->async_method_call( 1662336e96c6SChicago Duan respHandler, "xyz.openbmc_project.Logging", 1663336e96c6SChicago Duan "/xyz/openbmc_project/logging/entry/" + entryID, 1664336e96c6SChicago Duan "xyz.openbmc_project.Object.Delete", "Delete"); 16657e860f15SJohn Edward Broadbent }); 1666400fd1fbSAdriana Kobylak } 1667400fd1fbSAdriana Kobylak 16687e860f15SJohn Edward Broadbent inline void requestRoutesDBusEventLogEntryDownload(App& app) 1669400fd1fbSAdriana Kobylak { 16700fda0f12SGeorge Liu BMCWEB_ROUTE( 16710fda0f12SGeorge Liu app, 16720fda0f12SGeorge Liu "/redfish/v1/Systems/system/LogServices/EventLog/Entries/<str>/attachment") 1673ed398213SEd Tanous .privileges(redfish::privileges::getLogEntry) 16747e860f15SJohn Edward Broadbent .methods(boost::beast::http::verb::get)( 167545ca1b86SEd Tanous [&app](const crow::Request& req, 16767e860f15SJohn Edward Broadbent const std::shared_ptr<bmcweb::AsyncResp>& asyncResp, 167745ca1b86SEd Tanous const std::string& param) { 16783ba00073SCarson Labrado if (!redfish::setUpRedfishRoute(app, req, asyncResp)) 16797e860f15SJohn Edward Broadbent { 168045ca1b86SEd Tanous return; 168145ca1b86SEd Tanous } 1682002d39b4SEd Tanous if (!http_helpers::isOctetAccepted(req.getHeaderValue("Accept"))) 1683400fd1fbSAdriana Kobylak { 1684002d39b4SEd Tanous asyncResp->res.result(boost::beast::http::status::bad_request); 1685400fd1fbSAdriana Kobylak return; 1686400fd1fbSAdriana Kobylak } 1687400fd1fbSAdriana Kobylak 16887e860f15SJohn Edward Broadbent std::string entryID = param; 1689400fd1fbSAdriana Kobylak dbus::utility::escapePathForDbus(entryID); 1690400fd1fbSAdriana Kobylak 1691400fd1fbSAdriana Kobylak crow::connections::systemBus->async_method_call( 1692002d39b4SEd Tanous [asyncResp, entryID](const boost::system::error_code ec, 1693400fd1fbSAdriana Kobylak const sdbusplus::message::unix_fd& unixfd) { 1694400fd1fbSAdriana Kobylak if (ec.value() == EBADR) 1695400fd1fbSAdriana Kobylak { 1696002d39b4SEd Tanous messages::resourceNotFound(asyncResp->res, "EventLogAttachment", 1697002d39b4SEd Tanous entryID); 1698400fd1fbSAdriana Kobylak return; 1699400fd1fbSAdriana Kobylak } 1700400fd1fbSAdriana Kobylak if (ec) 1701400fd1fbSAdriana Kobylak { 1702400fd1fbSAdriana Kobylak BMCWEB_LOG_DEBUG << "DBUS response error " << ec; 1703400fd1fbSAdriana Kobylak messages::internalError(asyncResp->res); 1704400fd1fbSAdriana Kobylak return; 1705400fd1fbSAdriana Kobylak } 1706400fd1fbSAdriana Kobylak 1707400fd1fbSAdriana Kobylak int fd = -1; 1708400fd1fbSAdriana Kobylak fd = dup(unixfd); 1709400fd1fbSAdriana Kobylak if (fd == -1) 1710400fd1fbSAdriana Kobylak { 1711400fd1fbSAdriana Kobylak messages::internalError(asyncResp->res); 1712400fd1fbSAdriana Kobylak return; 1713400fd1fbSAdriana Kobylak } 1714400fd1fbSAdriana Kobylak 1715400fd1fbSAdriana Kobylak long long int size = lseek(fd, 0, SEEK_END); 1716400fd1fbSAdriana Kobylak if (size == -1) 1717400fd1fbSAdriana Kobylak { 1718400fd1fbSAdriana Kobylak messages::internalError(asyncResp->res); 1719400fd1fbSAdriana Kobylak return; 1720400fd1fbSAdriana Kobylak } 1721400fd1fbSAdriana Kobylak 1722400fd1fbSAdriana Kobylak // Arbitrary max size of 64kb 1723400fd1fbSAdriana Kobylak constexpr int maxFileSize = 65536; 1724400fd1fbSAdriana Kobylak if (size > maxFileSize) 1725400fd1fbSAdriana Kobylak { 1726002d39b4SEd Tanous BMCWEB_LOG_ERROR << "File size exceeds maximum allowed size of " 1727400fd1fbSAdriana Kobylak << maxFileSize; 1728400fd1fbSAdriana Kobylak messages::internalError(asyncResp->res); 1729400fd1fbSAdriana Kobylak return; 1730400fd1fbSAdriana Kobylak } 1731400fd1fbSAdriana Kobylak std::vector<char> data(static_cast<size_t>(size)); 1732400fd1fbSAdriana Kobylak long long int rc = lseek(fd, 0, SEEK_SET); 1733400fd1fbSAdriana Kobylak if (rc == -1) 1734400fd1fbSAdriana Kobylak { 1735400fd1fbSAdriana Kobylak messages::internalError(asyncResp->res); 1736400fd1fbSAdriana Kobylak return; 1737400fd1fbSAdriana Kobylak } 1738400fd1fbSAdriana Kobylak rc = read(fd, data.data(), data.size()); 1739400fd1fbSAdriana Kobylak if ((rc == -1) || (rc != size)) 1740400fd1fbSAdriana Kobylak { 1741400fd1fbSAdriana Kobylak messages::internalError(asyncResp->res); 1742400fd1fbSAdriana Kobylak return; 1743400fd1fbSAdriana Kobylak } 1744400fd1fbSAdriana Kobylak close(fd); 1745400fd1fbSAdriana Kobylak 1746400fd1fbSAdriana Kobylak std::string_view strData(data.data(), data.size()); 1747002d39b4SEd Tanous std::string output = crow::utility::base64encode(strData); 1748400fd1fbSAdriana Kobylak 1749400fd1fbSAdriana Kobylak asyncResp->res.addHeader("Content-Type", 1750400fd1fbSAdriana Kobylak "application/octet-stream"); 1751002d39b4SEd Tanous asyncResp->res.addHeader("Content-Transfer-Encoding", "Base64"); 1752400fd1fbSAdriana Kobylak asyncResp->res.body() = std::move(output); 1753400fd1fbSAdriana Kobylak }, 1754400fd1fbSAdriana Kobylak "xyz.openbmc_project.Logging", 1755400fd1fbSAdriana Kobylak "/xyz/openbmc_project/logging/entry/" + entryID, 1756400fd1fbSAdriana Kobylak "xyz.openbmc_project.Logging.Entry", "GetEntry"); 17577e860f15SJohn Edward Broadbent }); 17581da66f75SEd Tanous } 17591da66f75SEd Tanous 1760b7028ebfSSpencer Ku constexpr const char* hostLoggerFolderPath = "/var/log/console"; 1761b7028ebfSSpencer Ku 1762b7028ebfSSpencer Ku inline bool 1763b7028ebfSSpencer Ku getHostLoggerFiles(const std::string& hostLoggerFilePath, 1764b7028ebfSSpencer Ku std::vector<std::filesystem::path>& hostLoggerFiles) 1765b7028ebfSSpencer Ku { 1766b7028ebfSSpencer Ku std::error_code ec; 1767b7028ebfSSpencer Ku std::filesystem::directory_iterator logPath(hostLoggerFilePath, ec); 1768b7028ebfSSpencer Ku if (ec) 1769b7028ebfSSpencer Ku { 1770b7028ebfSSpencer Ku BMCWEB_LOG_ERROR << ec.message(); 1771b7028ebfSSpencer Ku return false; 1772b7028ebfSSpencer Ku } 1773b7028ebfSSpencer Ku for (const std::filesystem::directory_entry& it : logPath) 1774b7028ebfSSpencer Ku { 1775b7028ebfSSpencer Ku std::string filename = it.path().filename(); 1776b7028ebfSSpencer Ku // Prefix of each log files is "log". Find the file and save the 1777b7028ebfSSpencer Ku // path 177811ba3979SEd Tanous if (filename.starts_with("log")) 1779b7028ebfSSpencer Ku { 1780b7028ebfSSpencer Ku hostLoggerFiles.emplace_back(it.path()); 1781b7028ebfSSpencer Ku } 1782b7028ebfSSpencer Ku } 1783b7028ebfSSpencer Ku // As the log files rotate, they are appended with a ".#" that is higher for 1784b7028ebfSSpencer Ku // the older logs. Since we start from oldest logs, sort the name in 1785b7028ebfSSpencer Ku // descending order. 1786b7028ebfSSpencer Ku std::sort(hostLoggerFiles.rbegin(), hostLoggerFiles.rend(), 1787b7028ebfSSpencer Ku AlphanumLess<std::string>()); 1788b7028ebfSSpencer Ku 1789b7028ebfSSpencer Ku return true; 1790b7028ebfSSpencer Ku } 1791b7028ebfSSpencer Ku 179202cad96eSEd Tanous inline bool getHostLoggerEntries( 179302cad96eSEd Tanous const std::vector<std::filesystem::path>& hostLoggerFiles, uint64_t skip, 179402cad96eSEd Tanous uint64_t top, std::vector<std::string>& logEntries, size_t& logCount) 1795b7028ebfSSpencer Ku { 1796b7028ebfSSpencer Ku GzFileReader logFile; 1797b7028ebfSSpencer Ku 1798b7028ebfSSpencer Ku // Go though all log files and expose host logs. 1799b7028ebfSSpencer Ku for (const std::filesystem::path& it : hostLoggerFiles) 1800b7028ebfSSpencer Ku { 1801b7028ebfSSpencer Ku if (!logFile.gzGetLines(it.string(), skip, top, logEntries, logCount)) 1802b7028ebfSSpencer Ku { 1803b7028ebfSSpencer Ku BMCWEB_LOG_ERROR << "fail to expose host logs"; 1804b7028ebfSSpencer Ku return false; 1805b7028ebfSSpencer Ku } 1806b7028ebfSSpencer Ku } 1807b7028ebfSSpencer Ku // Get lastMessage from constructor by getter 1808b7028ebfSSpencer Ku std::string lastMessage = logFile.getLastMessage(); 1809b7028ebfSSpencer Ku if (!lastMessage.empty()) 1810b7028ebfSSpencer Ku { 1811b7028ebfSSpencer Ku logCount++; 1812b7028ebfSSpencer Ku if (logCount > skip && logCount <= (skip + top)) 1813b7028ebfSSpencer Ku { 1814b7028ebfSSpencer Ku logEntries.push_back(lastMessage); 1815b7028ebfSSpencer Ku } 1816b7028ebfSSpencer Ku } 1817b7028ebfSSpencer Ku return true; 1818b7028ebfSSpencer Ku } 1819b7028ebfSSpencer Ku 1820b7028ebfSSpencer Ku inline void fillHostLoggerEntryJson(const std::string& logEntryID, 1821b7028ebfSSpencer Ku const std::string& msg, 18226d6574c9SJason M. Bills nlohmann::json::object_t& logEntryJson) 1823b7028ebfSSpencer Ku { 1824b7028ebfSSpencer Ku // Fill in the log entry with the gathered data. 18256d6574c9SJason M. Bills logEntryJson["@odata.type"] = "#LogEntry.v1_4_0.LogEntry"; 18266d6574c9SJason M. Bills logEntryJson["@odata.id"] = 1827b7028ebfSSpencer Ku "/redfish/v1/Systems/system/LogServices/HostLogger/Entries/" + 18286d6574c9SJason M. Bills logEntryID; 18296d6574c9SJason M. Bills logEntryJson["Name"] = "Host Logger Entry"; 18306d6574c9SJason M. Bills logEntryJson["Id"] = logEntryID; 18316d6574c9SJason M. Bills logEntryJson["Message"] = msg; 18326d6574c9SJason M. Bills logEntryJson["EntryType"] = "Oem"; 18336d6574c9SJason M. Bills logEntryJson["Severity"] = "OK"; 18346d6574c9SJason M. Bills logEntryJson["OemRecordFormat"] = "Host Logger Entry"; 1835b7028ebfSSpencer Ku } 1836b7028ebfSSpencer Ku 1837b7028ebfSSpencer Ku inline void requestRoutesSystemHostLogger(App& app) 1838b7028ebfSSpencer Ku { 1839b7028ebfSSpencer Ku BMCWEB_ROUTE(app, "/redfish/v1/Systems/system/LogServices/HostLogger/") 1840b7028ebfSSpencer Ku .privileges(redfish::privileges::getLogService) 18411476687dSEd Tanous .methods(boost::beast::http::verb::get)( 18421476687dSEd Tanous [&app](const crow::Request& req, 18431476687dSEd Tanous const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) { 18443ba00073SCarson Labrado if (!redfish::setUpRedfishRoute(app, req, asyncResp)) 184545ca1b86SEd Tanous { 184645ca1b86SEd Tanous return; 184745ca1b86SEd Tanous } 1848b7028ebfSSpencer Ku asyncResp->res.jsonValue["@odata.id"] = 1849b7028ebfSSpencer Ku "/redfish/v1/Systems/system/LogServices/HostLogger"; 1850b7028ebfSSpencer Ku asyncResp->res.jsonValue["@odata.type"] = 1851b7028ebfSSpencer Ku "#LogService.v1_1_0.LogService"; 1852b7028ebfSSpencer Ku asyncResp->res.jsonValue["Name"] = "Host Logger Service"; 1853b7028ebfSSpencer Ku asyncResp->res.jsonValue["Description"] = "Host Logger Service"; 1854b7028ebfSSpencer Ku asyncResp->res.jsonValue["Id"] = "HostLogger"; 18551476687dSEd Tanous asyncResp->res.jsonValue["Entries"]["@odata.id"] = 18561476687dSEd Tanous "/redfish/v1/Systems/system/LogServices/HostLogger/Entries"; 1857b7028ebfSSpencer Ku }); 1858b7028ebfSSpencer Ku } 1859b7028ebfSSpencer Ku 1860b7028ebfSSpencer Ku inline void requestRoutesSystemHostLoggerCollection(App& app) 1861b7028ebfSSpencer Ku { 1862b7028ebfSSpencer Ku BMCWEB_ROUTE(app, 1863b7028ebfSSpencer Ku "/redfish/v1/Systems/system/LogServices/HostLogger/Entries/") 1864b7028ebfSSpencer Ku .privileges(redfish::privileges::getLogEntry) 1865002d39b4SEd Tanous .methods(boost::beast::http::verb::get)( 1866002d39b4SEd Tanous [&app](const crow::Request& req, 1867002d39b4SEd Tanous const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) { 1868c937d2bfSEd Tanous query_param::QueryCapabilities capabilities = { 1869c937d2bfSEd Tanous .canDelegateTop = true, 1870c937d2bfSEd Tanous .canDelegateSkip = true, 1871c937d2bfSEd Tanous }; 1872c937d2bfSEd Tanous query_param::Query delegatedQuery; 1873c937d2bfSEd Tanous if (!redfish::setUpRedfishRouteWithDelegation( 18743ba00073SCarson Labrado app, req, asyncResp, delegatedQuery, capabilities)) 1875b7028ebfSSpencer Ku { 1876b7028ebfSSpencer Ku return; 1877b7028ebfSSpencer Ku } 1878b7028ebfSSpencer Ku asyncResp->res.jsonValue["@odata.id"] = 1879b7028ebfSSpencer Ku "/redfish/v1/Systems/system/LogServices/HostLogger/Entries"; 1880b7028ebfSSpencer Ku asyncResp->res.jsonValue["@odata.type"] = 1881b7028ebfSSpencer Ku "#LogEntryCollection.LogEntryCollection"; 1882b7028ebfSSpencer Ku asyncResp->res.jsonValue["Name"] = "HostLogger Entries"; 1883b7028ebfSSpencer Ku asyncResp->res.jsonValue["Description"] = 1884b7028ebfSSpencer Ku "Collection of HostLogger Entries"; 18850fda0f12SGeorge Liu nlohmann::json& logEntryArray = asyncResp->res.jsonValue["Members"]; 1886b7028ebfSSpencer Ku logEntryArray = nlohmann::json::array(); 1887b7028ebfSSpencer Ku asyncResp->res.jsonValue["Members@odata.count"] = 0; 1888b7028ebfSSpencer Ku 1889b7028ebfSSpencer Ku std::vector<std::filesystem::path> hostLoggerFiles; 1890b7028ebfSSpencer Ku if (!getHostLoggerFiles(hostLoggerFolderPath, hostLoggerFiles)) 1891b7028ebfSSpencer Ku { 1892b7028ebfSSpencer Ku BMCWEB_LOG_ERROR << "fail to get host log file path"; 1893b7028ebfSSpencer Ku return; 1894b7028ebfSSpencer Ku } 1895b7028ebfSSpencer Ku 1896b7028ebfSSpencer Ku size_t logCount = 0; 1897b7028ebfSSpencer Ku // This vector only store the entries we want to expose that 1898b7028ebfSSpencer Ku // control by skip and top. 1899b7028ebfSSpencer Ku std::vector<std::string> logEntries; 1900c937d2bfSEd Tanous if (!getHostLoggerEntries(hostLoggerFiles, delegatedQuery.skip, 1901c937d2bfSEd Tanous delegatedQuery.top, logEntries, logCount)) 1902b7028ebfSSpencer Ku { 1903b7028ebfSSpencer Ku messages::internalError(asyncResp->res); 1904b7028ebfSSpencer Ku return; 1905b7028ebfSSpencer Ku } 1906b7028ebfSSpencer Ku // If vector is empty, that means skip value larger than total 1907b7028ebfSSpencer Ku // log count 190826f6976fSEd Tanous if (logEntries.empty()) 1909b7028ebfSSpencer Ku { 1910b7028ebfSSpencer Ku asyncResp->res.jsonValue["Members@odata.count"] = logCount; 1911b7028ebfSSpencer Ku return; 1912b7028ebfSSpencer Ku } 191326f6976fSEd Tanous if (!logEntries.empty()) 1914b7028ebfSSpencer Ku { 1915b7028ebfSSpencer Ku for (size_t i = 0; i < logEntries.size(); i++) 1916b7028ebfSSpencer Ku { 19176d6574c9SJason M. Bills nlohmann::json::object_t hostLogEntry; 1918002d39b4SEd Tanous fillHostLoggerEntryJson(std::to_string(delegatedQuery.skip + i), 1919002d39b4SEd Tanous logEntries[i], hostLogEntry); 19206d6574c9SJason M. Bills logEntryArray.push_back(std::move(hostLogEntry)); 1921b7028ebfSSpencer Ku } 1922b7028ebfSSpencer Ku 1923b7028ebfSSpencer Ku asyncResp->res.jsonValue["Members@odata.count"] = logCount; 1924c937d2bfSEd Tanous if (delegatedQuery.skip + delegatedQuery.top < logCount) 1925b7028ebfSSpencer Ku { 1926b7028ebfSSpencer Ku asyncResp->res.jsonValue["Members@odata.nextLink"] = 19270fda0f12SGeorge Liu "/redfish/v1/Systems/system/LogServices/HostLogger/Entries?$skip=" + 1928002d39b4SEd Tanous std::to_string(delegatedQuery.skip + delegatedQuery.top); 1929b7028ebfSSpencer Ku } 1930b7028ebfSSpencer Ku } 1931b7028ebfSSpencer Ku }); 1932b7028ebfSSpencer Ku } 1933b7028ebfSSpencer Ku 1934b7028ebfSSpencer Ku inline void requestRoutesSystemHostLoggerLogEntry(App& app) 1935b7028ebfSSpencer Ku { 1936b7028ebfSSpencer Ku BMCWEB_ROUTE( 1937b7028ebfSSpencer Ku app, "/redfish/v1/Systems/system/LogServices/HostLogger/Entries/<str>/") 1938b7028ebfSSpencer Ku .privileges(redfish::privileges::getLogEntry) 1939b7028ebfSSpencer Ku .methods(boost::beast::http::verb::get)( 194045ca1b86SEd Tanous [&app](const crow::Request& req, 1941b7028ebfSSpencer Ku const std::shared_ptr<bmcweb::AsyncResp>& asyncResp, 1942b7028ebfSSpencer Ku const std::string& param) { 19433ba00073SCarson Labrado if (!redfish::setUpRedfishRoute(app, req, asyncResp)) 194445ca1b86SEd Tanous { 194545ca1b86SEd Tanous return; 194645ca1b86SEd Tanous } 1947b7028ebfSSpencer Ku const std::string& targetID = param; 1948b7028ebfSSpencer Ku 1949b7028ebfSSpencer Ku uint64_t idInt = 0; 1950ca45aa3cSEd Tanous 1951ca45aa3cSEd Tanous // NOLINTNEXTLINE(cppcoreguidelines-pro-bounds-pointer-arithmetic) 1952ca45aa3cSEd Tanous const char* end = targetID.data() + targetID.size(); 1953ca45aa3cSEd Tanous 1954ca45aa3cSEd Tanous auto [ptr, ec] = std::from_chars(targetID.data(), end, idInt); 1955b7028ebfSSpencer Ku if (ec == std::errc::invalid_argument) 1956b7028ebfSSpencer Ku { 1957ace85d60SEd Tanous messages::resourceMissingAtURI(asyncResp->res, req.urlView); 1958b7028ebfSSpencer Ku return; 1959b7028ebfSSpencer Ku } 1960b7028ebfSSpencer Ku if (ec == std::errc::result_out_of_range) 1961b7028ebfSSpencer Ku { 1962ace85d60SEd Tanous messages::resourceMissingAtURI(asyncResp->res, req.urlView); 1963b7028ebfSSpencer Ku return; 1964b7028ebfSSpencer Ku } 1965b7028ebfSSpencer Ku 1966b7028ebfSSpencer Ku std::vector<std::filesystem::path> hostLoggerFiles; 1967b7028ebfSSpencer Ku if (!getHostLoggerFiles(hostLoggerFolderPath, hostLoggerFiles)) 1968b7028ebfSSpencer Ku { 1969b7028ebfSSpencer Ku BMCWEB_LOG_ERROR << "fail to get host log file path"; 1970b7028ebfSSpencer Ku return; 1971b7028ebfSSpencer Ku } 1972b7028ebfSSpencer Ku 1973b7028ebfSSpencer Ku size_t logCount = 0; 1974b7028ebfSSpencer Ku uint64_t top = 1; 1975b7028ebfSSpencer Ku std::vector<std::string> logEntries; 1976b7028ebfSSpencer Ku // We can get specific entry by skip and top. For example, if we 1977b7028ebfSSpencer Ku // want to get nth entry, we can set skip = n-1 and top = 1 to 1978b7028ebfSSpencer Ku // get that entry 1979002d39b4SEd Tanous if (!getHostLoggerEntries(hostLoggerFiles, idInt, top, logEntries, 1980002d39b4SEd Tanous logCount)) 1981b7028ebfSSpencer Ku { 1982b7028ebfSSpencer Ku messages::internalError(asyncResp->res); 1983b7028ebfSSpencer Ku return; 1984b7028ebfSSpencer Ku } 1985b7028ebfSSpencer Ku 1986b7028ebfSSpencer Ku if (!logEntries.empty()) 1987b7028ebfSSpencer Ku { 19886d6574c9SJason M. Bills nlohmann::json::object_t hostLogEntry; 19896d6574c9SJason M. Bills fillHostLoggerEntryJson(targetID, logEntries[0], hostLogEntry); 19906d6574c9SJason M. Bills asyncResp->res.jsonValue.update(hostLogEntry); 1991b7028ebfSSpencer Ku return; 1992b7028ebfSSpencer Ku } 1993b7028ebfSSpencer Ku 1994b7028ebfSSpencer Ku // Requested ID was not found 1995ace85d60SEd Tanous messages::resourceMissingAtURI(asyncResp->res, req.urlView); 1996b7028ebfSSpencer Ku }); 1997b7028ebfSSpencer Ku } 1998b7028ebfSSpencer Ku 1999fdd26906SClaire Weinan constexpr char const* dumpManagerIface = 2000fdd26906SClaire Weinan "xyz.openbmc_project.Collection.DeleteAll"; 2001fdd26906SClaire Weinan inline void handleLogServicesCollectionGet( 2002fdd26906SClaire Weinan crow::App& app, const crow::Request& req, 2003fdd26906SClaire Weinan const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) 20041da66f75SEd Tanous { 20053ba00073SCarson Labrado if (!redfish::setUpRedfishRoute(app, req, asyncResp)) 200645ca1b86SEd Tanous { 200745ca1b86SEd Tanous return; 200845ca1b86SEd Tanous } 20097e860f15SJohn Edward Broadbent // Collections don't include the static data added by SubRoute 20107e860f15SJohn Edward Broadbent // because it has a duplicate entry for members 2011e1f26343SJason M. Bills asyncResp->res.jsonValue["@odata.type"] = 20121da66f75SEd Tanous "#LogServiceCollection.LogServiceCollection"; 2013e1f26343SJason M. Bills asyncResp->res.jsonValue["@odata.id"] = 2014e1f26343SJason M. Bills "/redfish/v1/Managers/bmc/LogServices"; 2015002d39b4SEd Tanous asyncResp->res.jsonValue["Name"] = "Open BMC Log Services Collection"; 2016e1f26343SJason M. Bills asyncResp->res.jsonValue["Description"] = 20171da66f75SEd Tanous "Collection of LogServices for this Manager"; 2018002d39b4SEd Tanous nlohmann::json& logServiceArray = asyncResp->res.jsonValue["Members"]; 2019c4bf6374SJason M. Bills logServiceArray = nlohmann::json::array(); 2020fdd26906SClaire Weinan 2021c4bf6374SJason M. Bills #ifdef BMCWEB_ENABLE_REDFISH_BMC_JOURNAL 2022c4bf6374SJason M. Bills logServiceArray.push_back( 2023002d39b4SEd Tanous {{"@odata.id", "/redfish/v1/Managers/bmc/LogServices/Journal"}}); 2024c4bf6374SJason M. Bills #endif 2025fdd26906SClaire Weinan 2026fdd26906SClaire Weinan asyncResp->res.jsonValue["Members@odata.count"] = logServiceArray.size(); 2027fdd26906SClaire Weinan 2028fdd26906SClaire Weinan #ifdef BMCWEB_ENABLE_REDFISH_DUMP_LOG 2029fdd26906SClaire Weinan auto respHandler = 2030fdd26906SClaire Weinan [asyncResp]( 2031fdd26906SClaire Weinan const boost::system::error_code ec, 2032fdd26906SClaire Weinan const dbus::utility::MapperGetSubTreePathsResponse& subTreePaths) { 2033fdd26906SClaire Weinan if (ec) 2034fdd26906SClaire Weinan { 2035fdd26906SClaire Weinan BMCWEB_LOG_ERROR 2036fdd26906SClaire Weinan << "handleLogServicesCollectionGet respHandler got error " 2037fdd26906SClaire Weinan << ec; 2038fdd26906SClaire Weinan // Assume that getting an error simply means there are no dump 2039fdd26906SClaire Weinan // LogServices. Return without adding any error response. 2040fdd26906SClaire Weinan return; 2041fdd26906SClaire Weinan } 2042fdd26906SClaire Weinan 2043fdd26906SClaire Weinan nlohmann::json& logServiceArrayLocal = 2044fdd26906SClaire Weinan asyncResp->res.jsonValue["Members"]; 2045fdd26906SClaire Weinan 2046fdd26906SClaire Weinan for (const std::string& path : subTreePaths) 2047fdd26906SClaire Weinan { 2048fdd26906SClaire Weinan if (path == "/xyz/openbmc_project/dump/bmc") 2049fdd26906SClaire Weinan { 2050fdd26906SClaire Weinan logServiceArrayLocal.push_back( 2051fdd26906SClaire Weinan {{"@odata.id", 2052fdd26906SClaire Weinan "/redfish/v1/Managers/bmc/LogServices/Dump"}}); 2053fdd26906SClaire Weinan } 2054fdd26906SClaire Weinan else if (path == "/xyz/openbmc_project/dump/faultlog") 2055fdd26906SClaire Weinan { 2056fdd26906SClaire Weinan logServiceArrayLocal.push_back( 2057fdd26906SClaire Weinan {{"@odata.id", 2058fdd26906SClaire Weinan "/redfish/v1/Managers/bmc/LogServices/FaultLog"}}); 2059fdd26906SClaire Weinan } 2060fdd26906SClaire Weinan } 2061fdd26906SClaire Weinan 2062e1f26343SJason M. Bills asyncResp->res.jsonValue["Members@odata.count"] = 2063fdd26906SClaire Weinan logServiceArrayLocal.size(); 2064fdd26906SClaire Weinan }; 2065fdd26906SClaire Weinan 2066fdd26906SClaire Weinan crow::connections::systemBus->async_method_call( 2067fdd26906SClaire Weinan respHandler, "xyz.openbmc_project.ObjectMapper", 2068fdd26906SClaire Weinan "/xyz/openbmc_project/object_mapper", 2069fdd26906SClaire Weinan "xyz.openbmc_project.ObjectMapper", "GetSubTreePaths", 2070fdd26906SClaire Weinan "/xyz/openbmc_project/dump", 0, 2071fdd26906SClaire Weinan std::array<const char*, 1>{dumpManagerIface}); 2072fdd26906SClaire Weinan #endif 2073fdd26906SClaire Weinan } 2074fdd26906SClaire Weinan 2075fdd26906SClaire Weinan inline void requestRoutesBMCLogServiceCollection(App& app) 2076fdd26906SClaire Weinan { 2077fdd26906SClaire Weinan BMCWEB_ROUTE(app, "/redfish/v1/Managers/bmc/LogServices/") 2078fdd26906SClaire Weinan .privileges(redfish::privileges::getLogServiceCollection) 2079fdd26906SClaire Weinan .methods(boost::beast::http::verb::get)( 2080fdd26906SClaire Weinan std::bind_front(handleLogServicesCollectionGet, std::ref(app))); 2081e1f26343SJason M. Bills } 2082e1f26343SJason M. Bills 20837e860f15SJohn Edward Broadbent inline void requestRoutesBMCJournalLogService(App& app) 2084e1f26343SJason M. Bills { 20857e860f15SJohn Edward Broadbent BMCWEB_ROUTE(app, "/redfish/v1/Managers/bmc/LogServices/Journal/") 2086ed398213SEd Tanous .privileges(redfish::privileges::getLogService) 20877e860f15SJohn Edward Broadbent .methods(boost::beast::http::verb::get)( 208845ca1b86SEd Tanous [&app](const crow::Request& req, 208945ca1b86SEd Tanous const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) { 20903ba00073SCarson Labrado if (!redfish::setUpRedfishRoute(app, req, asyncResp)) 20917e860f15SJohn Edward Broadbent { 209245ca1b86SEd Tanous return; 209345ca1b86SEd Tanous } 2094e1f26343SJason M. Bills asyncResp->res.jsonValue["@odata.type"] = 2095e1f26343SJason M. Bills "#LogService.v1_1_0.LogService"; 20960f74e643SEd Tanous asyncResp->res.jsonValue["@odata.id"] = 20970f74e643SEd Tanous "/redfish/v1/Managers/bmc/LogServices/Journal"; 2098002d39b4SEd Tanous asyncResp->res.jsonValue["Name"] = "Open BMC Journal Log Service"; 2099002d39b4SEd Tanous asyncResp->res.jsonValue["Description"] = "BMC Journal Log Service"; 2100c4bf6374SJason M. Bills asyncResp->res.jsonValue["Id"] = "BMC Journal"; 2101e1f26343SJason M. Bills asyncResp->res.jsonValue["OverWritePolicy"] = "WrapsWhenFull"; 21027c8c4058STejas Patil 21037c8c4058STejas Patil std::pair<std::string, std::string> redfishDateTimeOffset = 21047c8c4058STejas Patil crow::utility::getDateTimeOffsetNow(); 2105002d39b4SEd Tanous asyncResp->res.jsonValue["DateTime"] = redfishDateTimeOffset.first; 21067c8c4058STejas Patil asyncResp->res.jsonValue["DateTimeLocalOffset"] = 21077c8c4058STejas Patil redfishDateTimeOffset.second; 21087c8c4058STejas Patil 21091476687dSEd Tanous asyncResp->res.jsonValue["Entries"]["@odata.id"] = 21101476687dSEd Tanous "/redfish/v1/Managers/bmc/LogServices/Journal/Entries"; 21117e860f15SJohn Edward Broadbent }); 2112e1f26343SJason M. Bills } 2113e1f26343SJason M. Bills 21143a48b3a2SJason M. Bills static int 21153a48b3a2SJason M. Bills fillBMCJournalLogEntryJson(const std::string& bmcJournalLogEntryID, 2116e1f26343SJason M. Bills sd_journal* journal, 21173a48b3a2SJason M. Bills nlohmann::json::object_t& bmcJournalLogEntryJson) 2118e1f26343SJason M. Bills { 2119e1f26343SJason M. Bills // Get the Log Entry contents 2120e1f26343SJason M. Bills int ret = 0; 2121e1f26343SJason M. Bills 2122a8fe54f0SJason M. Bills std::string message; 2123a8fe54f0SJason M. Bills std::string_view syslogID; 2124a8fe54f0SJason M. Bills ret = getJournalMetadata(journal, "SYSLOG_IDENTIFIER", syslogID); 2125a8fe54f0SJason M. Bills if (ret < 0) 2126a8fe54f0SJason M. Bills { 2127a8fe54f0SJason M. Bills BMCWEB_LOG_ERROR << "Failed to read SYSLOG_IDENTIFIER field: " 2128a8fe54f0SJason M. Bills << strerror(-ret); 2129a8fe54f0SJason M. Bills } 2130a8fe54f0SJason M. Bills if (!syslogID.empty()) 2131a8fe54f0SJason M. Bills { 2132a8fe54f0SJason M. Bills message += std::string(syslogID) + ": "; 2133a8fe54f0SJason M. Bills } 2134a8fe54f0SJason M. Bills 213539e77504SEd Tanous std::string_view msg; 213616428a1aSJason M. Bills ret = getJournalMetadata(journal, "MESSAGE", msg); 2137e1f26343SJason M. Bills if (ret < 0) 2138e1f26343SJason M. Bills { 2139e1f26343SJason M. Bills BMCWEB_LOG_ERROR << "Failed to read MESSAGE field: " << strerror(-ret); 2140e1f26343SJason M. Bills return 1; 2141e1f26343SJason M. Bills } 2142a8fe54f0SJason M. Bills message += std::string(msg); 2143e1f26343SJason M. Bills 2144e1f26343SJason M. Bills // Get the severity from the PRIORITY field 2145271584abSEd Tanous long int severity = 8; // Default to an invalid priority 214616428a1aSJason M. Bills ret = getJournalMetadata(journal, "PRIORITY", 10, severity); 2147e1f26343SJason M. Bills if (ret < 0) 2148e1f26343SJason M. Bills { 2149e1f26343SJason M. Bills BMCWEB_LOG_ERROR << "Failed to read PRIORITY field: " << strerror(-ret); 2150e1f26343SJason M. Bills } 2151e1f26343SJason M. Bills 2152e1f26343SJason M. Bills // Get the Created time from the timestamp 215316428a1aSJason M. Bills std::string entryTimeStr; 215416428a1aSJason M. Bills if (!getEntryTimestamp(journal, entryTimeStr)) 2155e1f26343SJason M. Bills { 215616428a1aSJason M. Bills return 1; 2157e1f26343SJason M. Bills } 2158e1f26343SJason M. Bills 2159e1f26343SJason M. Bills // Fill in the log entry with the gathered data 216084afc48bSJason M. Bills bmcJournalLogEntryJson["@odata.type"] = "#LogEntry.v1_8_0.LogEntry"; 216184afc48bSJason M. Bills bmcJournalLogEntryJson["@odata.id"] = 216284afc48bSJason M. Bills "/redfish/v1/Managers/bmc/LogServices/Journal/Entries/" + 216384afc48bSJason M. Bills bmcJournalLogEntryID; 216484afc48bSJason M. Bills bmcJournalLogEntryJson["Name"] = "BMC Journal Entry"; 216584afc48bSJason M. Bills bmcJournalLogEntryJson["Id"] = bmcJournalLogEntryID; 216684afc48bSJason M. Bills bmcJournalLogEntryJson["Message"] = std::move(message); 216784afc48bSJason M. Bills bmcJournalLogEntryJson["EntryType"] = "Oem"; 216884afc48bSJason M. Bills bmcJournalLogEntryJson["Severity"] = severity <= 2 ? "Critical" 2169738c1e61SPatrick Williams : severity <= 4 ? "Warning" 217084afc48bSJason M. Bills : "OK"; 217184afc48bSJason M. Bills bmcJournalLogEntryJson["OemRecordFormat"] = "BMC Journal Entry"; 217284afc48bSJason M. Bills bmcJournalLogEntryJson["Created"] = std::move(entryTimeStr); 2173e1f26343SJason M. Bills return 0; 2174e1f26343SJason M. Bills } 2175e1f26343SJason M. Bills 21767e860f15SJohn Edward Broadbent inline void requestRoutesBMCJournalLogEntryCollection(App& app) 2177e1f26343SJason M. Bills { 21787e860f15SJohn Edward Broadbent BMCWEB_ROUTE(app, "/redfish/v1/Managers/bmc/LogServices/Journal/Entries/") 2179ed398213SEd Tanous .privileges(redfish::privileges::getLogEntryCollection) 2180002d39b4SEd Tanous .methods(boost::beast::http::verb::get)( 2181002d39b4SEd Tanous [&app](const crow::Request& req, 2182002d39b4SEd Tanous const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) { 2183c937d2bfSEd Tanous query_param::QueryCapabilities capabilities = { 2184c937d2bfSEd Tanous .canDelegateTop = true, 2185c937d2bfSEd Tanous .canDelegateSkip = true, 2186c937d2bfSEd Tanous }; 2187c937d2bfSEd Tanous query_param::Query delegatedQuery; 2188c937d2bfSEd Tanous if (!redfish::setUpRedfishRouteWithDelegation( 21893ba00073SCarson Labrado app, req, asyncResp, delegatedQuery, capabilities)) 2190193ad2faSJason M. Bills { 2191193ad2faSJason M. Bills return; 2192193ad2faSJason M. Bills } 21937e860f15SJohn Edward Broadbent // Collections don't include the static data added by SubRoute 21947e860f15SJohn Edward Broadbent // because it has a duplicate entry for members 2195e1f26343SJason M. Bills asyncResp->res.jsonValue["@odata.type"] = 2196e1f26343SJason M. Bills "#LogEntryCollection.LogEntryCollection"; 21970f74e643SEd Tanous asyncResp->res.jsonValue["@odata.id"] = 21980f74e643SEd Tanous "/redfish/v1/Managers/bmc/LogServices/Journal/Entries"; 2199e1f26343SJason M. Bills asyncResp->res.jsonValue["Name"] = "Open BMC Journal Entries"; 2200e1f26343SJason M. Bills asyncResp->res.jsonValue["Description"] = 2201e1f26343SJason M. Bills "Collection of BMC Journal Entries"; 22020fda0f12SGeorge Liu nlohmann::json& logEntryArray = asyncResp->res.jsonValue["Members"]; 2203e1f26343SJason M. Bills logEntryArray = nlohmann::json::array(); 2204e1f26343SJason M. Bills 22057e860f15SJohn Edward Broadbent // Go through the journal and use the timestamp to create a 22067e860f15SJohn Edward Broadbent // unique ID for each entry 2207e1f26343SJason M. Bills sd_journal* journalTmp = nullptr; 2208e1f26343SJason M. Bills int ret = sd_journal_open(&journalTmp, SD_JOURNAL_LOCAL_ONLY); 2209e1f26343SJason M. Bills if (ret < 0) 2210e1f26343SJason M. Bills { 2211002d39b4SEd Tanous BMCWEB_LOG_ERROR << "failed to open journal: " << strerror(-ret); 2212f12894f8SJason M. Bills messages::internalError(asyncResp->res); 2213e1f26343SJason M. Bills return; 2214e1f26343SJason M. Bills } 22150fda0f12SGeorge Liu std::unique_ptr<sd_journal, decltype(&sd_journal_close)> journal( 22160fda0f12SGeorge Liu journalTmp, sd_journal_close); 2217e1f26343SJason M. Bills journalTmp = nullptr; 2218b01bf299SEd Tanous uint64_t entryCount = 0; 2219e85d6b16SJason M. Bills // Reset the unique ID on the first entry 2220e85d6b16SJason M. Bills bool firstEntry = true; 2221e1f26343SJason M. Bills SD_JOURNAL_FOREACH(journal.get()) 2222e1f26343SJason M. Bills { 2223193ad2faSJason M. Bills entryCount++; 22247e860f15SJohn Edward Broadbent // Handle paging using skip (number of entries to skip from 22257e860f15SJohn Edward Broadbent // the start) and top (number of entries to display) 2226c937d2bfSEd Tanous if (entryCount <= delegatedQuery.skip || 2227c937d2bfSEd Tanous entryCount > delegatedQuery.skip + delegatedQuery.top) 2228193ad2faSJason M. Bills { 2229193ad2faSJason M. Bills continue; 2230193ad2faSJason M. Bills } 2231193ad2faSJason M. Bills 223216428a1aSJason M. Bills std::string idStr; 2233e85d6b16SJason M. Bills if (!getUniqueEntryID(journal.get(), idStr, firstEntry)) 2234e1f26343SJason M. Bills { 2235e1f26343SJason M. Bills continue; 2236e1f26343SJason M. Bills } 2237e85d6b16SJason M. Bills firstEntry = false; 2238e85d6b16SJason M. Bills 22393a48b3a2SJason M. Bills nlohmann::json::object_t bmcJournalLogEntry; 2240c4bf6374SJason M. Bills if (fillBMCJournalLogEntryJson(idStr, journal.get(), 2241c4bf6374SJason M. Bills bmcJournalLogEntry) != 0) 2242e1f26343SJason M. Bills { 2243f12894f8SJason M. Bills messages::internalError(asyncResp->res); 2244e1f26343SJason M. Bills return; 2245e1f26343SJason M. Bills } 22463a48b3a2SJason M. Bills logEntryArray.push_back(std::move(bmcJournalLogEntry)); 2247e1f26343SJason M. Bills } 2248193ad2faSJason M. Bills asyncResp->res.jsonValue["Members@odata.count"] = entryCount; 2249c937d2bfSEd Tanous if (delegatedQuery.skip + delegatedQuery.top < entryCount) 2250193ad2faSJason M. Bills { 2251193ad2faSJason M. Bills asyncResp->res.jsonValue["Members@odata.nextLink"] = 22520fda0f12SGeorge Liu "/redfish/v1/Managers/bmc/LogServices/Journal/Entries?$skip=" + 2253c937d2bfSEd Tanous std::to_string(delegatedQuery.skip + delegatedQuery.top); 2254193ad2faSJason M. Bills } 22557e860f15SJohn Edward Broadbent }); 2256e1f26343SJason M. Bills } 2257e1f26343SJason M. Bills 22587e860f15SJohn Edward Broadbent inline void requestRoutesBMCJournalLogEntry(App& app) 2259e1f26343SJason M. Bills { 22607e860f15SJohn Edward Broadbent BMCWEB_ROUTE(app, 22617e860f15SJohn Edward Broadbent "/redfish/v1/Managers/bmc/LogServices/Journal/Entries/<str>/") 2262ed398213SEd Tanous .privileges(redfish::privileges::getLogEntry) 22637e860f15SJohn Edward Broadbent .methods(boost::beast::http::verb::get)( 226445ca1b86SEd Tanous [&app](const crow::Request& req, 22657e860f15SJohn Edward Broadbent const std::shared_ptr<bmcweb::AsyncResp>& asyncResp, 22667e860f15SJohn Edward Broadbent const std::string& entryID) { 22673ba00073SCarson Labrado if (!redfish::setUpRedfishRoute(app, req, asyncResp)) 226845ca1b86SEd Tanous { 226945ca1b86SEd Tanous return; 227045ca1b86SEd Tanous } 2271e1f26343SJason M. Bills // Convert the unique ID back to a timestamp to find the entry 2272e1f26343SJason M. Bills uint64_t ts = 0; 2273271584abSEd Tanous uint64_t index = 0; 22748d1b46d7Szhanghch05 if (!getTimestampFromID(asyncResp, entryID, ts, index)) 2275e1f26343SJason M. Bills { 227616428a1aSJason M. Bills return; 2277e1f26343SJason M. Bills } 2278e1f26343SJason M. Bills 2279e1f26343SJason M. Bills sd_journal* journalTmp = nullptr; 2280e1f26343SJason M. Bills int ret = sd_journal_open(&journalTmp, SD_JOURNAL_LOCAL_ONLY); 2281e1f26343SJason M. Bills if (ret < 0) 2282e1f26343SJason M. Bills { 2283002d39b4SEd Tanous BMCWEB_LOG_ERROR << "failed to open journal: " << strerror(-ret); 2284f12894f8SJason M. Bills messages::internalError(asyncResp->res); 2285e1f26343SJason M. Bills return; 2286e1f26343SJason M. Bills } 2287002d39b4SEd Tanous std::unique_ptr<sd_journal, decltype(&sd_journal_close)> journal( 2288002d39b4SEd Tanous journalTmp, sd_journal_close); 2289e1f26343SJason M. Bills journalTmp = nullptr; 22907e860f15SJohn Edward Broadbent // Go to the timestamp in the log and move to the entry at the 22917e860f15SJohn Edward Broadbent // index tracking the unique ID 2292af07e3f5SJason M. Bills std::string idStr; 2293af07e3f5SJason M. Bills bool firstEntry = true; 2294e1f26343SJason M. Bills ret = sd_journal_seek_realtime_usec(journal.get(), ts); 22952056b6d1SManojkiran Eda if (ret < 0) 22962056b6d1SManojkiran Eda { 22972056b6d1SManojkiran Eda BMCWEB_LOG_ERROR << "failed to seek to an entry in journal" 22982056b6d1SManojkiran Eda << strerror(-ret); 22992056b6d1SManojkiran Eda messages::internalError(asyncResp->res); 23002056b6d1SManojkiran Eda return; 23012056b6d1SManojkiran Eda } 2302271584abSEd Tanous for (uint64_t i = 0; i <= index; i++) 2303e1f26343SJason M. Bills { 2304e1f26343SJason M. Bills sd_journal_next(journal.get()); 2305af07e3f5SJason M. Bills if (!getUniqueEntryID(journal.get(), idStr, firstEntry)) 2306af07e3f5SJason M. Bills { 2307af07e3f5SJason M. Bills messages::internalError(asyncResp->res); 2308af07e3f5SJason M. Bills return; 2309af07e3f5SJason M. Bills } 2310af07e3f5SJason M. Bills firstEntry = false; 2311af07e3f5SJason M. Bills } 2312c4bf6374SJason M. Bills // Confirm that the entry ID matches what was requested 2313af07e3f5SJason M. Bills if (idStr != entryID) 2314c4bf6374SJason M. Bills { 2315ace85d60SEd Tanous messages::resourceMissingAtURI(asyncResp->res, req.urlView); 2316c4bf6374SJason M. Bills return; 2317c4bf6374SJason M. Bills } 2318c4bf6374SJason M. Bills 23193a48b3a2SJason M. Bills nlohmann::json::object_t bmcJournalLogEntry; 2320c4bf6374SJason M. Bills if (fillBMCJournalLogEntryJson(entryID, journal.get(), 23213a48b3a2SJason M. Bills bmcJournalLogEntry) != 0) 2322e1f26343SJason M. Bills { 2323f12894f8SJason M. Bills messages::internalError(asyncResp->res); 2324e1f26343SJason M. Bills return; 2325e1f26343SJason M. Bills } 2326d405bb51SJason M. Bills asyncResp->res.jsonValue.update(bmcJournalLogEntry); 23277e860f15SJohn Edward Broadbent }); 2328c9bb6861Sraviteja-b } 2329c9bb6861Sraviteja-b 2330fdd26906SClaire Weinan inline void 2331fdd26906SClaire Weinan getDumpServiceInfo(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp, 2332fdd26906SClaire Weinan const std::string& dumpType) 2333c9bb6861Sraviteja-b { 2334fdd26906SClaire Weinan std::string dumpPath; 2335fdd26906SClaire Weinan std::string overWritePolicy; 2336fdd26906SClaire Weinan bool collectDiagnosticDataSupported = false; 2337fdd26906SClaire Weinan 2338fdd26906SClaire Weinan if (dumpType == "BMC") 233945ca1b86SEd Tanous { 2340fdd26906SClaire Weinan dumpPath = "/redfish/v1/Managers/bmc/LogServices/Dump"; 2341fdd26906SClaire Weinan overWritePolicy = "WrapsWhenFull"; 2342fdd26906SClaire Weinan collectDiagnosticDataSupported = true; 2343fdd26906SClaire Weinan } 2344fdd26906SClaire Weinan else if (dumpType == "FaultLog") 2345fdd26906SClaire Weinan { 2346fdd26906SClaire Weinan dumpPath = "/redfish/v1/Managers/bmc/LogServices/FaultLog"; 2347fdd26906SClaire Weinan overWritePolicy = "Unknown"; 2348fdd26906SClaire Weinan collectDiagnosticDataSupported = false; 2349fdd26906SClaire Weinan } 2350fdd26906SClaire Weinan else if (dumpType == "System") 2351fdd26906SClaire Weinan { 2352fdd26906SClaire Weinan dumpPath = "/redfish/v1/Systems/system/LogServices/Dump"; 2353fdd26906SClaire Weinan overWritePolicy = "WrapsWhenFull"; 2354fdd26906SClaire Weinan collectDiagnosticDataSupported = true; 2355fdd26906SClaire Weinan } 2356fdd26906SClaire Weinan else 2357fdd26906SClaire Weinan { 2358fdd26906SClaire Weinan BMCWEB_LOG_ERROR << "getDumpServiceInfo() invalid dump type: " 2359fdd26906SClaire Weinan << dumpType; 2360fdd26906SClaire Weinan messages::internalError(asyncResp->res); 236145ca1b86SEd Tanous return; 236245ca1b86SEd Tanous } 2363fdd26906SClaire Weinan 2364fdd26906SClaire Weinan asyncResp->res.jsonValue["@odata.id"] = dumpPath; 2365fdd26906SClaire Weinan asyncResp->res.jsonValue["@odata.type"] = "#LogService.v1_2_0.LogService"; 2366c9bb6861Sraviteja-b asyncResp->res.jsonValue["Name"] = "Dump LogService"; 2367fdd26906SClaire Weinan asyncResp->res.jsonValue["Description"] = dumpType + " Dump LogService"; 2368fdd26906SClaire Weinan asyncResp->res.jsonValue["Id"] = std::filesystem::path(dumpPath).filename(); 2369fdd26906SClaire Weinan asyncResp->res.jsonValue["OverWritePolicy"] = std::move(overWritePolicy); 23707c8c4058STejas Patil 23717c8c4058STejas Patil std::pair<std::string, std::string> redfishDateTimeOffset = 23727c8c4058STejas Patil crow::utility::getDateTimeOffsetNow(); 23730fda0f12SGeorge Liu asyncResp->res.jsonValue["DateTime"] = redfishDateTimeOffset.first; 23747c8c4058STejas Patil asyncResp->res.jsonValue["DateTimeLocalOffset"] = 23757c8c4058STejas Patil redfishDateTimeOffset.second; 23767c8c4058STejas Patil 2377fdd26906SClaire Weinan asyncResp->res.jsonValue["Entries"]["@odata.id"] = dumpPath + "/Entries"; 2378002d39b4SEd Tanous asyncResp->res.jsonValue["Actions"]["#LogService.ClearLog"]["target"] = 2379fdd26906SClaire Weinan dumpPath + "/Actions/LogService.ClearLog"; 2380fdd26906SClaire Weinan 2381fdd26906SClaire Weinan if (collectDiagnosticDataSupported) 2382fdd26906SClaire Weinan { 2383002d39b4SEd Tanous asyncResp->res.jsonValue["Actions"]["#LogService.CollectDiagnosticData"] 23841476687dSEd Tanous ["target"] = 2385fdd26906SClaire Weinan dumpPath + "/Actions/LogService.CollectDiagnosticData"; 2386fdd26906SClaire Weinan } 2387c9bb6861Sraviteja-b } 2388c9bb6861Sraviteja-b 2389fdd26906SClaire Weinan inline void handleLogServicesDumpServiceGet( 2390fdd26906SClaire Weinan crow::App& app, const std::string& dumpType, const crow::Request& req, 2391fdd26906SClaire Weinan const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) 23927e860f15SJohn Edward Broadbent { 23933ba00073SCarson Labrado if (!redfish::setUpRedfishRoute(app, req, asyncResp)) 239445ca1b86SEd Tanous { 239545ca1b86SEd Tanous return; 239645ca1b86SEd Tanous } 2397fdd26906SClaire Weinan getDumpServiceInfo(asyncResp, dumpType); 2398fdd26906SClaire Weinan } 2399c9bb6861Sraviteja-b 2400fdd26906SClaire Weinan inline void handleLogServicesDumpEntriesCollectionGet( 2401fdd26906SClaire Weinan crow::App& app, const std::string& dumpType, const crow::Request& req, 2402fdd26906SClaire Weinan const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) 2403fdd26906SClaire Weinan { 2404fdd26906SClaire Weinan if (!redfish::setUpRedfishRoute(app, req, asyncResp)) 2405fdd26906SClaire Weinan { 2406fdd26906SClaire Weinan return; 2407fdd26906SClaire Weinan } 2408fdd26906SClaire Weinan getDumpEntryCollection(asyncResp, dumpType); 2409fdd26906SClaire Weinan } 2410fdd26906SClaire Weinan 2411fdd26906SClaire Weinan inline void handleLogServicesDumpEntryGet( 2412fdd26906SClaire Weinan crow::App& app, const std::string& dumpType, const crow::Request& req, 2413fdd26906SClaire Weinan const std::shared_ptr<bmcweb::AsyncResp>& asyncResp, 2414fdd26906SClaire Weinan const std::string& dumpId) 2415fdd26906SClaire Weinan { 2416fdd26906SClaire Weinan if (!redfish::setUpRedfishRoute(app, req, asyncResp)) 2417fdd26906SClaire Weinan { 2418fdd26906SClaire Weinan return; 2419fdd26906SClaire Weinan } 2420fdd26906SClaire Weinan getDumpEntryById(asyncResp, dumpId, dumpType); 2421fdd26906SClaire Weinan } 2422fdd26906SClaire Weinan 2423fdd26906SClaire Weinan inline void handleLogServicesDumpEntryDelete( 2424fdd26906SClaire Weinan crow::App& app, const std::string& dumpType, const crow::Request& req, 2425fdd26906SClaire Weinan const std::shared_ptr<bmcweb::AsyncResp>& asyncResp, 2426fdd26906SClaire Weinan const std::string& dumpId) 2427fdd26906SClaire Weinan { 2428fdd26906SClaire Weinan if (!redfish::setUpRedfishRoute(app, req, asyncResp)) 2429fdd26906SClaire Weinan { 2430fdd26906SClaire Weinan return; 2431fdd26906SClaire Weinan } 2432fdd26906SClaire Weinan deleteDumpEntry(asyncResp, dumpId, dumpType); 2433fdd26906SClaire Weinan } 2434fdd26906SClaire Weinan 2435fdd26906SClaire Weinan inline void handleLogServicesDumpCollectDiagnosticDataPost( 2436fdd26906SClaire Weinan crow::App& app, const std::string& dumpType, const crow::Request& req, 2437fdd26906SClaire Weinan const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) 2438fdd26906SClaire Weinan { 2439fdd26906SClaire Weinan if (!redfish::setUpRedfishRoute(app, req, asyncResp)) 2440fdd26906SClaire Weinan { 2441fdd26906SClaire Weinan return; 2442fdd26906SClaire Weinan } 2443fdd26906SClaire Weinan createDump(asyncResp, req, dumpType); 2444fdd26906SClaire Weinan } 2445fdd26906SClaire Weinan 2446fdd26906SClaire Weinan inline void handleLogServicesDumpClearLogPost( 2447fdd26906SClaire Weinan crow::App& app, const std::string& dumpType, const crow::Request& req, 2448fdd26906SClaire Weinan const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) 2449fdd26906SClaire Weinan { 2450fdd26906SClaire Weinan if (!redfish::setUpRedfishRoute(app, req, asyncResp)) 2451fdd26906SClaire Weinan { 2452fdd26906SClaire Weinan return; 2453fdd26906SClaire Weinan } 2454fdd26906SClaire Weinan clearDump(asyncResp, dumpType); 2455fdd26906SClaire Weinan } 2456fdd26906SClaire Weinan 2457fdd26906SClaire Weinan inline void requestRoutesBMCDumpService(App& app) 2458fdd26906SClaire Weinan { 2459fdd26906SClaire Weinan BMCWEB_ROUTE(app, "/redfish/v1/Managers/bmc/LogServices/Dump/") 2460fdd26906SClaire Weinan .privileges(redfish::privileges::getLogService) 2461fdd26906SClaire Weinan .methods(boost::beast::http::verb::get)(std::bind_front( 2462fdd26906SClaire Weinan handleLogServicesDumpServiceGet, std::ref(app), "BMC")); 2463fdd26906SClaire Weinan } 2464fdd26906SClaire Weinan 2465fdd26906SClaire Weinan inline void requestRoutesBMCDumpEntryCollection(App& app) 2466fdd26906SClaire Weinan { 2467fdd26906SClaire Weinan BMCWEB_ROUTE(app, "/redfish/v1/Managers/bmc/LogServices/Dump/Entries/") 2468fdd26906SClaire Weinan .privileges(redfish::privileges::getLogEntryCollection) 2469fdd26906SClaire Weinan .methods(boost::beast::http::verb::get)(std::bind_front( 2470fdd26906SClaire Weinan handleLogServicesDumpEntriesCollectionGet, std::ref(app), "BMC")); 2471c9bb6861Sraviteja-b } 2472c9bb6861Sraviteja-b 24737e860f15SJohn Edward Broadbent inline void requestRoutesBMCDumpEntry(App& app) 2474c9bb6861Sraviteja-b { 24757e860f15SJohn Edward Broadbent BMCWEB_ROUTE(app, 24767e860f15SJohn Edward Broadbent "/redfish/v1/Managers/bmc/LogServices/Dump/Entries/<str>/") 2477ed398213SEd Tanous .privileges(redfish::privileges::getLogEntry) 2478fdd26906SClaire Weinan .methods(boost::beast::http::verb::get)(std::bind_front( 2479fdd26906SClaire Weinan handleLogServicesDumpEntryGet, std::ref(app), "BMC")); 2480fdd26906SClaire Weinan 24817e860f15SJohn Edward Broadbent BMCWEB_ROUTE(app, 24827e860f15SJohn Edward Broadbent "/redfish/v1/Managers/bmc/LogServices/Dump/Entries/<str>/") 2483ed398213SEd Tanous .privileges(redfish::privileges::deleteLogEntry) 2484fdd26906SClaire Weinan .methods(boost::beast::http::verb::delete_)(std::bind_front( 2485fdd26906SClaire Weinan handleLogServicesDumpEntryDelete, std::ref(app), "BMC")); 2486c9bb6861Sraviteja-b } 2487c9bb6861Sraviteja-b 24887e860f15SJohn Edward Broadbent inline void requestRoutesBMCDumpCreate(App& app) 2489c9bb6861Sraviteja-b { 24900fda0f12SGeorge Liu BMCWEB_ROUTE( 24910fda0f12SGeorge Liu app, 24920fda0f12SGeorge Liu "/redfish/v1/Managers/bmc/LogServices/Dump/Actions/LogService.CollectDiagnosticData/") 2493ed398213SEd Tanous .privileges(redfish::privileges::postLogService) 24947e860f15SJohn Edward Broadbent .methods(boost::beast::http::verb::post)( 2495fdd26906SClaire Weinan std::bind_front(handleLogServicesDumpCollectDiagnosticDataPost, 2496fdd26906SClaire Weinan std::ref(app), "BMC")); 2497a43be80fSAsmitha Karunanithi } 2498a43be80fSAsmitha Karunanithi 24997e860f15SJohn Edward Broadbent inline void requestRoutesBMCDumpClear(App& app) 250080319af1SAsmitha Karunanithi { 25010fda0f12SGeorge Liu BMCWEB_ROUTE( 25020fda0f12SGeorge Liu app, 25030fda0f12SGeorge Liu "/redfish/v1/Managers/bmc/LogServices/Dump/Actions/LogService.ClearLog/") 2504ed398213SEd Tanous .privileges(redfish::privileges::postLogService) 2505fdd26906SClaire Weinan .methods(boost::beast::http::verb::post)(std::bind_front( 2506fdd26906SClaire Weinan handleLogServicesDumpClearLogPost, std::ref(app), "BMC")); 250745ca1b86SEd Tanous } 2508fdd26906SClaire Weinan 2509fdd26906SClaire Weinan inline void requestRoutesFaultLogDumpService(App& app) 2510fdd26906SClaire Weinan { 2511fdd26906SClaire Weinan BMCWEB_ROUTE(app, "/redfish/v1/Managers/bmc/LogServices/FaultLog/") 2512fdd26906SClaire Weinan .privileges(redfish::privileges::getLogService) 2513fdd26906SClaire Weinan .methods(boost::beast::http::verb::get)(std::bind_front( 2514fdd26906SClaire Weinan handleLogServicesDumpServiceGet, std::ref(app), "FaultLog")); 2515fdd26906SClaire Weinan } 2516fdd26906SClaire Weinan 2517fdd26906SClaire Weinan inline void requestRoutesFaultLogDumpEntryCollection(App& app) 2518fdd26906SClaire Weinan { 2519fdd26906SClaire Weinan BMCWEB_ROUTE(app, "/redfish/v1/Managers/bmc/LogServices/FaultLog/Entries/") 2520fdd26906SClaire Weinan .privileges(redfish::privileges::getLogEntryCollection) 2521fdd26906SClaire Weinan .methods(boost::beast::http::verb::get)( 2522fdd26906SClaire Weinan std::bind_front(handleLogServicesDumpEntriesCollectionGet, 2523fdd26906SClaire Weinan std::ref(app), "FaultLog")); 2524fdd26906SClaire Weinan } 2525fdd26906SClaire Weinan 2526fdd26906SClaire Weinan inline void requestRoutesFaultLogDumpEntry(App& app) 2527fdd26906SClaire Weinan { 2528fdd26906SClaire Weinan BMCWEB_ROUTE(app, 2529fdd26906SClaire Weinan "/redfish/v1/Managers/bmc/LogServices/FaultLog/Entries/<str>/") 2530fdd26906SClaire Weinan .privileges(redfish::privileges::getLogEntry) 2531fdd26906SClaire Weinan .methods(boost::beast::http::verb::get)(std::bind_front( 2532fdd26906SClaire Weinan handleLogServicesDumpEntryGet, std::ref(app), "FaultLog")); 2533fdd26906SClaire Weinan 2534fdd26906SClaire Weinan BMCWEB_ROUTE(app, 2535fdd26906SClaire Weinan "/redfish/v1/Managers/bmc/LogServices/FaultLog/Entries/<str>/") 2536fdd26906SClaire Weinan .privileges(redfish::privileges::deleteLogEntry) 2537fdd26906SClaire Weinan .methods(boost::beast::http::verb::delete_)(std::bind_front( 2538fdd26906SClaire Weinan handleLogServicesDumpEntryDelete, std::ref(app), "FaultLog")); 2539fdd26906SClaire Weinan } 2540fdd26906SClaire Weinan 2541fdd26906SClaire Weinan inline void requestRoutesFaultLogDumpClear(App& app) 2542fdd26906SClaire Weinan { 2543fdd26906SClaire Weinan BMCWEB_ROUTE( 2544fdd26906SClaire Weinan app, 2545fdd26906SClaire Weinan "/redfish/v1/Managers/bmc/LogServices/FaultLog/Actions/LogService.ClearLog/") 2546fdd26906SClaire Weinan .privileges(redfish::privileges::postLogService) 2547fdd26906SClaire Weinan .methods(boost::beast::http::verb::post)(std::bind_front( 2548fdd26906SClaire Weinan handleLogServicesDumpClearLogPost, std::ref(app), "FaultLog")); 25495cb1dd27SAsmitha Karunanithi } 25505cb1dd27SAsmitha Karunanithi 25517e860f15SJohn Edward Broadbent inline void requestRoutesSystemDumpService(App& app) 25525cb1dd27SAsmitha Karunanithi { 25537e860f15SJohn Edward Broadbent BMCWEB_ROUTE(app, "/redfish/v1/Systems/system/LogServices/Dump/") 2554ed398213SEd Tanous .privileges(redfish::privileges::getLogService) 2555002d39b4SEd Tanous .methods(boost::beast::http::verb::get)( 2556002d39b4SEd Tanous [&app](const crow::Request& req, 2557002d39b4SEd Tanous const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) { 25583ba00073SCarson Labrado if (!redfish::setUpRedfishRoute(app, req, asyncResp)) 25597e860f15SJohn Edward Broadbent { 256045ca1b86SEd Tanous return; 256145ca1b86SEd Tanous } 25625cb1dd27SAsmitha Karunanithi asyncResp->res.jsonValue["@odata.id"] = 25635cb1dd27SAsmitha Karunanithi "/redfish/v1/Systems/system/LogServices/Dump"; 25645cb1dd27SAsmitha Karunanithi asyncResp->res.jsonValue["@odata.type"] = 2565d337bb72SAsmitha Karunanithi "#LogService.v1_2_0.LogService"; 25665cb1dd27SAsmitha Karunanithi asyncResp->res.jsonValue["Name"] = "Dump LogService"; 256745ca1b86SEd Tanous asyncResp->res.jsonValue["Description"] = "System Dump LogService"; 25685cb1dd27SAsmitha Karunanithi asyncResp->res.jsonValue["Id"] = "Dump"; 25695cb1dd27SAsmitha Karunanithi asyncResp->res.jsonValue["OverWritePolicy"] = "WrapsWhenFull"; 25707c8c4058STejas Patil 25717c8c4058STejas Patil std::pair<std::string, std::string> redfishDateTimeOffset = 25727c8c4058STejas Patil crow::utility::getDateTimeOffsetNow(); 257345ca1b86SEd Tanous asyncResp->res.jsonValue["DateTime"] = redfishDateTimeOffset.first; 25747c8c4058STejas Patil asyncResp->res.jsonValue["DateTimeLocalOffset"] = 25757c8c4058STejas Patil redfishDateTimeOffset.second; 25767c8c4058STejas Patil 25771476687dSEd Tanous asyncResp->res.jsonValue["Entries"]["@odata.id"] = 25781476687dSEd Tanous "/redfish/v1/Systems/system/LogServices/Dump/Entries"; 2579002d39b4SEd Tanous asyncResp->res.jsonValue["Actions"]["#LogService.ClearLog"]["target"] = 25801476687dSEd Tanous "/redfish/v1/Systems/system/LogServices/Dump/Actions/LogService.ClearLog"; 25811476687dSEd Tanous 2582002d39b4SEd Tanous asyncResp->res.jsonValue["Actions"]["#LogService.CollectDiagnosticData"] 25831476687dSEd Tanous ["target"] = 25841476687dSEd Tanous "/redfish/v1/Systems/system/LogServices/Dump/Actions/LogService.CollectDiagnosticData"; 25857e860f15SJohn Edward Broadbent }); 25865cb1dd27SAsmitha Karunanithi } 25875cb1dd27SAsmitha Karunanithi 25887e860f15SJohn Edward Broadbent inline void requestRoutesSystemDumpEntryCollection(App& app) 25897e860f15SJohn Edward Broadbent { 25907e860f15SJohn Edward Broadbent 25915cb1dd27SAsmitha Karunanithi /** 25925cb1dd27SAsmitha Karunanithi * Functions triggers appropriate requests on DBus 25935cb1dd27SAsmitha Karunanithi */ 2594b2a3289dSAsmitha Karunanithi BMCWEB_ROUTE(app, "/redfish/v1/Systems/system/LogServices/Dump/Entries/") 2595ed398213SEd Tanous .privileges(redfish::privileges::getLogEntryCollection) 25967e860f15SJohn Edward Broadbent .methods(boost::beast::http::verb::get)( 259745ca1b86SEd Tanous [&app](const crow::Request& req, 2598864d6a17SJohn Edward Broadbent const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) { 25993ba00073SCarson Labrado if (!redfish::setUpRedfishRoute(app, req, asyncResp)) 260045ca1b86SEd Tanous { 260145ca1b86SEd Tanous return; 260245ca1b86SEd Tanous } 26035cb1dd27SAsmitha Karunanithi getDumpEntryCollection(asyncResp, "System"); 26047e860f15SJohn Edward Broadbent }); 26055cb1dd27SAsmitha Karunanithi } 26065cb1dd27SAsmitha Karunanithi 26077e860f15SJohn Edward Broadbent inline void requestRoutesSystemDumpEntry(App& app) 26085cb1dd27SAsmitha Karunanithi { 26097e860f15SJohn Edward Broadbent BMCWEB_ROUTE(app, 2610864d6a17SJohn Edward Broadbent "/redfish/v1/Systems/system/LogServices/Dump/Entries/<str>/") 2611ed398213SEd Tanous .privileges(redfish::privileges::getLogEntry) 2612ed398213SEd Tanous 26137e860f15SJohn Edward Broadbent .methods(boost::beast::http::verb::get)( 261445ca1b86SEd Tanous [&app](const crow::Request& req, 26157e860f15SJohn Edward Broadbent const std::shared_ptr<bmcweb::AsyncResp>& asyncResp, 26167e860f15SJohn Edward Broadbent const std::string& param) { 26173ba00073SCarson Labrado if (!redfish::setUpRedfishRoute(app, req, asyncResp)) 2618c7a6d660SClaire Weinan { 2619c7a6d660SClaire Weinan return; 2620c7a6d660SClaire Weinan } 2621c7a6d660SClaire Weinan getDumpEntryById(asyncResp, param, "System"); 26227e860f15SJohn Edward Broadbent }); 26238d1b46d7Szhanghch05 26247e860f15SJohn Edward Broadbent BMCWEB_ROUTE(app, 2625864d6a17SJohn Edward Broadbent "/redfish/v1/Systems/system/LogServices/Dump/Entries/<str>/") 2626ed398213SEd Tanous .privileges(redfish::privileges::deleteLogEntry) 26277e860f15SJohn Edward Broadbent .methods(boost::beast::http::verb::delete_)( 262845ca1b86SEd Tanous [&app](const crow::Request& req, 26297e860f15SJohn Edward Broadbent const std::shared_ptr<bmcweb::AsyncResp>& asyncResp, 26307e860f15SJohn Edward Broadbent const std::string& param) { 26313ba00073SCarson Labrado if (!redfish::setUpRedfishRoute(app, req, asyncResp)) 263245ca1b86SEd Tanous { 263345ca1b86SEd Tanous return; 263445ca1b86SEd Tanous } 26357e860f15SJohn Edward Broadbent deleteDumpEntry(asyncResp, param, "system"); 26367e860f15SJohn Edward Broadbent }); 26375cb1dd27SAsmitha Karunanithi } 2638c9bb6861Sraviteja-b 26397e860f15SJohn Edward Broadbent inline void requestRoutesSystemDumpCreate(App& app) 2640c9bb6861Sraviteja-b { 26410fda0f12SGeorge Liu BMCWEB_ROUTE( 26420fda0f12SGeorge Liu app, 26430fda0f12SGeorge Liu "/redfish/v1/Systems/system/LogServices/Dump/Actions/LogService.CollectDiagnosticData/") 2644ed398213SEd Tanous .privileges(redfish::privileges::postLogService) 26457e860f15SJohn Edward Broadbent .methods(boost::beast::http::verb::post)( 264645ca1b86SEd Tanous [&app](const crow::Request& req, 264745ca1b86SEd Tanous const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) { 26483ba00073SCarson Labrado if (!redfish::setUpRedfishRoute(app, req, asyncResp)) 264945ca1b86SEd Tanous { 265045ca1b86SEd Tanous return; 265145ca1b86SEd Tanous } 265245ca1b86SEd Tanous createDump(asyncResp, req, "System"); 265345ca1b86SEd Tanous }); 2654a43be80fSAsmitha Karunanithi } 2655a43be80fSAsmitha Karunanithi 26567e860f15SJohn Edward Broadbent inline void requestRoutesSystemDumpClear(App& app) 2657a43be80fSAsmitha Karunanithi { 26580fda0f12SGeorge Liu BMCWEB_ROUTE( 26590fda0f12SGeorge Liu app, 26600fda0f12SGeorge Liu "/redfish/v1/Systems/system/LogServices/Dump/Actions/LogService.ClearLog/") 2661ed398213SEd Tanous .privileges(redfish::privileges::postLogService) 26627e860f15SJohn Edward Broadbent .methods(boost::beast::http::verb::post)( 266345ca1b86SEd Tanous [&app](const crow::Request& req, 26647e860f15SJohn Edward Broadbent const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) 26657e860f15SJohn Edward Broadbent 266645ca1b86SEd Tanous { 26673ba00073SCarson Labrado if (!redfish::setUpRedfishRoute(app, req, asyncResp)) 266845ca1b86SEd Tanous { 266945ca1b86SEd Tanous return; 267045ca1b86SEd Tanous } 267145ca1b86SEd Tanous clearDump(asyncResp, "System"); 267245ca1b86SEd Tanous }); 2673013487e5Sraviteja-b } 2674013487e5Sraviteja-b 26757e860f15SJohn Edward Broadbent inline void requestRoutesCrashdumpService(App& app) 26761da66f75SEd Tanous { 26773946028dSAppaRao Puli // Note: Deviated from redfish privilege registry for GET & HEAD 26783946028dSAppaRao Puli // method for security reasons. 26791da66f75SEd Tanous /** 26801da66f75SEd Tanous * Functions triggers appropriate requests on DBus 26811da66f75SEd Tanous */ 26827e860f15SJohn Edward Broadbent BMCWEB_ROUTE(app, "/redfish/v1/Systems/system/LogServices/Crashdump/") 2683ed398213SEd Tanous // This is incorrect, should be: 2684ed398213SEd Tanous //.privileges(redfish::privileges::getLogService) 2685432a890cSEd Tanous .privileges({{"ConfigureManager"}}) 2686002d39b4SEd Tanous .methods(boost::beast::http::verb::get)( 2687002d39b4SEd Tanous [&app](const crow::Request& req, 2688002d39b4SEd Tanous const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) { 26893ba00073SCarson Labrado if (!redfish::setUpRedfishRoute(app, req, asyncResp)) 269045ca1b86SEd Tanous { 269145ca1b86SEd Tanous return; 269245ca1b86SEd Tanous } 26937e860f15SJohn Edward Broadbent // Copy over the static data to include the entries added by 26947e860f15SJohn Edward Broadbent // SubRoute 26950f74e643SEd Tanous asyncResp->res.jsonValue["@odata.id"] = 2696424c4176SJason M. Bills "/redfish/v1/Systems/system/LogServices/Crashdump"; 2697e1f26343SJason M. Bills asyncResp->res.jsonValue["@odata.type"] = 26988e6c099aSJason M. Bills "#LogService.v1_2_0.LogService"; 26994f50ae4bSGunnar Mills asyncResp->res.jsonValue["Name"] = "Open BMC Oem Crashdump Service"; 27004f50ae4bSGunnar Mills asyncResp->res.jsonValue["Description"] = "Oem Crashdump Service"; 27014f50ae4bSGunnar Mills asyncResp->res.jsonValue["Id"] = "Oem Crashdump"; 2702e1f26343SJason M. Bills asyncResp->res.jsonValue["OverWritePolicy"] = "WrapsWhenFull"; 2703e1f26343SJason M. Bills asyncResp->res.jsonValue["MaxNumberOfRecords"] = 3; 27047c8c4058STejas Patil 27057c8c4058STejas Patil std::pair<std::string, std::string> redfishDateTimeOffset = 27067c8c4058STejas Patil crow::utility::getDateTimeOffsetNow(); 27077c8c4058STejas Patil asyncResp->res.jsonValue["DateTime"] = redfishDateTimeOffset.first; 27087c8c4058STejas Patil asyncResp->res.jsonValue["DateTimeLocalOffset"] = 27097c8c4058STejas Patil redfishDateTimeOffset.second; 27107c8c4058STejas Patil 27111476687dSEd Tanous asyncResp->res.jsonValue["Entries"]["@odata.id"] = 27121476687dSEd Tanous "/redfish/v1/Systems/system/LogServices/Crashdump/Entries"; 2713002d39b4SEd Tanous asyncResp->res.jsonValue["Actions"]["#LogService.ClearLog"]["target"] = 27141476687dSEd Tanous "/redfish/v1/Systems/system/LogServices/Crashdump/Actions/LogService.ClearLog"; 2715002d39b4SEd Tanous asyncResp->res.jsonValue["Actions"]["#LogService.CollectDiagnosticData"] 27161476687dSEd Tanous ["target"] = 27171476687dSEd Tanous "/redfish/v1/Systems/system/LogServices/Crashdump/Actions/LogService.CollectDiagnosticData"; 27187e860f15SJohn Edward Broadbent }); 27191da66f75SEd Tanous } 27201da66f75SEd Tanous 27217e860f15SJohn Edward Broadbent void inline requestRoutesCrashdumpClear(App& app) 27225b61b5e8SJason M. Bills { 27230fda0f12SGeorge Liu BMCWEB_ROUTE( 27240fda0f12SGeorge Liu app, 27250fda0f12SGeorge Liu "/redfish/v1/Systems/system/LogServices/Crashdump/Actions/LogService.ClearLog/") 2726ed398213SEd Tanous // This is incorrect, should be: 2727ed398213SEd Tanous //.privileges(redfish::privileges::postLogService) 2728432a890cSEd Tanous .privileges({{"ConfigureComponents"}}) 27297e860f15SJohn Edward Broadbent .methods(boost::beast::http::verb::post)( 273045ca1b86SEd Tanous [&app](const crow::Request& req, 27317e860f15SJohn Edward Broadbent const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) { 27323ba00073SCarson Labrado if (!redfish::setUpRedfishRoute(app, req, asyncResp)) 273345ca1b86SEd Tanous { 273445ca1b86SEd Tanous return; 273545ca1b86SEd Tanous } 27365b61b5e8SJason M. Bills crow::connections::systemBus->async_method_call( 27375b61b5e8SJason M. Bills [asyncResp](const boost::system::error_code ec, 2738cb13a392SEd Tanous const std::string&) { 27395b61b5e8SJason M. Bills if (ec) 27405b61b5e8SJason M. Bills { 27415b61b5e8SJason M. Bills messages::internalError(asyncResp->res); 27425b61b5e8SJason M. Bills return; 27435b61b5e8SJason M. Bills } 27445b61b5e8SJason M. Bills messages::success(asyncResp->res); 27455b61b5e8SJason M. Bills }, 2746002d39b4SEd Tanous crashdumpObject, crashdumpPath, deleteAllInterface, "DeleteAll"); 27477e860f15SJohn Edward Broadbent }); 27485b61b5e8SJason M. Bills } 27495b61b5e8SJason M. Bills 27508d1b46d7Szhanghch05 static void 27518d1b46d7Szhanghch05 logCrashdumpEntry(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp, 27528d1b46d7Szhanghch05 const std::string& logID, nlohmann::json& logEntryJson) 2753e855dd28SJason M. Bills { 2754043a0536SJohnathan Mantey auto getStoredLogCallback = 2755b9d36b47SEd Tanous [asyncResp, logID, 2756b9d36b47SEd Tanous &logEntryJson](const boost::system::error_code ec, 2757b9d36b47SEd Tanous const dbus::utility::DBusPropertiesMap& params) { 2758e855dd28SJason M. Bills if (ec) 2759e855dd28SJason M. Bills { 2760e855dd28SJason M. Bills BMCWEB_LOG_DEBUG << "failed to get log ec: " << ec.message(); 27611ddcf01aSJason M. Bills if (ec.value() == 27621ddcf01aSJason M. Bills boost::system::linux_error::bad_request_descriptor) 27631ddcf01aSJason M. Bills { 2764002d39b4SEd Tanous messages::resourceNotFound(asyncResp->res, "LogEntry", logID); 27651ddcf01aSJason M. Bills } 27661ddcf01aSJason M. Bills else 27671ddcf01aSJason M. Bills { 2768e855dd28SJason M. Bills messages::internalError(asyncResp->res); 27691ddcf01aSJason M. Bills } 2770e855dd28SJason M. Bills return; 2771e855dd28SJason M. Bills } 2772043a0536SJohnathan Mantey 2773043a0536SJohnathan Mantey std::string timestamp{}; 2774043a0536SJohnathan Mantey std::string filename{}; 2775043a0536SJohnathan Mantey std::string logfile{}; 27762c70f800SEd Tanous parseCrashdumpParameters(params, filename, timestamp, logfile); 2777043a0536SJohnathan Mantey 2778043a0536SJohnathan Mantey if (filename.empty() || timestamp.empty()) 2779e855dd28SJason M. Bills { 2780002d39b4SEd Tanous messages::resourceMissingAtURI(asyncResp->res, 2781002d39b4SEd Tanous crow::utility::urlFromPieces(logID)); 2782e855dd28SJason M. Bills return; 2783e855dd28SJason M. Bills } 2784e855dd28SJason M. Bills 2785043a0536SJohnathan Mantey std::string crashdumpURI = 2786e855dd28SJason M. Bills "/redfish/v1/Systems/system/LogServices/Crashdump/Entries/" + 2787043a0536SJohnathan Mantey logID + "/" + filename; 278884afc48bSJason M. Bills nlohmann::json::object_t logEntry; 278984afc48bSJason M. Bills logEntry["@odata.type"] = "#LogEntry.v1_7_0.LogEntry"; 279084afc48bSJason M. Bills logEntry["@odata.id"] = 279184afc48bSJason M. Bills "/redfish/v1/Systems/system/LogServices/Crashdump/Entries/" + logID; 279284afc48bSJason M. Bills logEntry["Name"] = "CPU Crashdump"; 279384afc48bSJason M. Bills logEntry["Id"] = logID; 279484afc48bSJason M. Bills logEntry["EntryType"] = "Oem"; 279584afc48bSJason M. Bills logEntry["AdditionalDataURI"] = std::move(crashdumpURI); 279684afc48bSJason M. Bills logEntry["DiagnosticDataType"] = "OEM"; 279784afc48bSJason M. Bills logEntry["OEMDiagnosticDataType"] = "PECICrashdump"; 279884afc48bSJason M. Bills logEntry["Created"] = std::move(timestamp); 27992b20ef6eSJason M. Bills 28002b20ef6eSJason M. Bills // If logEntryJson references an array of LogEntry resources 28012b20ef6eSJason M. Bills // ('Members' list), then push this as a new entry, otherwise set it 28022b20ef6eSJason M. Bills // directly 28032b20ef6eSJason M. Bills if (logEntryJson.is_array()) 28042b20ef6eSJason M. Bills { 28052b20ef6eSJason M. Bills logEntryJson.push_back(logEntry); 28062b20ef6eSJason M. Bills asyncResp->res.jsonValue["Members@odata.count"] = 28072b20ef6eSJason M. Bills logEntryJson.size(); 28082b20ef6eSJason M. Bills } 28092b20ef6eSJason M. Bills else 28102b20ef6eSJason M. Bills { 2811d405bb51SJason M. Bills logEntryJson.update(logEntry); 28122b20ef6eSJason M. Bills } 2813e855dd28SJason M. Bills }; 2814e855dd28SJason M. Bills crow::connections::systemBus->async_method_call( 28155b61b5e8SJason M. Bills std::move(getStoredLogCallback), crashdumpObject, 28165b61b5e8SJason M. Bills crashdumpPath + std::string("/") + logID, 2817043a0536SJohnathan Mantey "org.freedesktop.DBus.Properties", "GetAll", crashdumpInterface); 2818e855dd28SJason M. Bills } 2819e855dd28SJason M. Bills 28207e860f15SJohn Edward Broadbent inline void requestRoutesCrashdumpEntryCollection(App& app) 28211da66f75SEd Tanous { 28223946028dSAppaRao Puli // Note: Deviated from redfish privilege registry for GET & HEAD 28233946028dSAppaRao Puli // method for security reasons. 28241da66f75SEd Tanous /** 28251da66f75SEd Tanous * Functions triggers appropriate requests on DBus 28261da66f75SEd Tanous */ 28277e860f15SJohn Edward Broadbent BMCWEB_ROUTE(app, 28287e860f15SJohn Edward Broadbent "/redfish/v1/Systems/system/LogServices/Crashdump/Entries/") 2829ed398213SEd Tanous // This is incorrect, should be. 2830ed398213SEd Tanous //.privileges(redfish::privileges::postLogEntryCollection) 2831432a890cSEd Tanous .privileges({{"ConfigureComponents"}}) 2832002d39b4SEd Tanous .methods(boost::beast::http::verb::get)( 2833002d39b4SEd Tanous [&app](const crow::Request& req, 2834002d39b4SEd Tanous const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) { 28353ba00073SCarson Labrado if (!redfish::setUpRedfishRoute(app, req, asyncResp)) 283645ca1b86SEd Tanous { 283745ca1b86SEd Tanous return; 283845ca1b86SEd Tanous } 28392b20ef6eSJason M. Bills crow::connections::systemBus->async_method_call( 28402b20ef6eSJason M. Bills [asyncResp](const boost::system::error_code ec, 28412b20ef6eSJason M. Bills const std::vector<std::string>& resp) { 28421da66f75SEd Tanous if (ec) 28431da66f75SEd Tanous { 28441da66f75SEd Tanous if (ec.value() != 28451da66f75SEd Tanous boost::system::errc::no_such_file_or_directory) 28461da66f75SEd Tanous { 28471da66f75SEd Tanous BMCWEB_LOG_DEBUG << "failed to get entries ec: " 28481da66f75SEd Tanous << ec.message(); 2849f12894f8SJason M. Bills messages::internalError(asyncResp->res); 28501da66f75SEd Tanous return; 28511da66f75SEd Tanous } 28521da66f75SEd Tanous } 2853e1f26343SJason M. Bills asyncResp->res.jsonValue["@odata.type"] = 28541da66f75SEd Tanous "#LogEntryCollection.LogEntryCollection"; 28550f74e643SEd Tanous asyncResp->res.jsonValue["@odata.id"] = 2856424c4176SJason M. Bills "/redfish/v1/Systems/system/LogServices/Crashdump/Entries"; 2857002d39b4SEd Tanous asyncResp->res.jsonValue["Name"] = "Open BMC Crashdump Entries"; 2858e1f26343SJason M. Bills asyncResp->res.jsonValue["Description"] = 2859424c4176SJason M. Bills "Collection of Crashdump Entries"; 2860002d39b4SEd Tanous asyncResp->res.jsonValue["Members"] = nlohmann::json::array(); 2861a2dd60a6SBrandon Kim asyncResp->res.jsonValue["Members@odata.count"] = 0; 28622b20ef6eSJason M. Bills 28632b20ef6eSJason M. Bills for (const std::string& path : resp) 28641da66f75SEd Tanous { 28652b20ef6eSJason M. Bills const sdbusplus::message::object_path objPath(path); 2866e855dd28SJason M. Bills // Get the log ID 28672b20ef6eSJason M. Bills std::string logID = objPath.filename(); 28682b20ef6eSJason M. Bills if (logID.empty()) 28691da66f75SEd Tanous { 2870e855dd28SJason M. Bills continue; 28711da66f75SEd Tanous } 2872e855dd28SJason M. Bills // Add the log entry to the array 28732b20ef6eSJason M. Bills logCrashdumpEntry(asyncResp, logID, 28742b20ef6eSJason M. Bills asyncResp->res.jsonValue["Members"]); 28751da66f75SEd Tanous } 28762b20ef6eSJason M. Bills }, 28771da66f75SEd Tanous "xyz.openbmc_project.ObjectMapper", 28781da66f75SEd Tanous "/xyz/openbmc_project/object_mapper", 28791da66f75SEd Tanous "xyz.openbmc_project.ObjectMapper", "GetSubTreePaths", "", 0, 28805b61b5e8SJason M. Bills std::array<const char*, 1>{crashdumpInterface}); 28817e860f15SJohn Edward Broadbent }); 28821da66f75SEd Tanous } 28831da66f75SEd Tanous 28847e860f15SJohn Edward Broadbent inline void requestRoutesCrashdumpEntry(App& app) 28851da66f75SEd Tanous { 28863946028dSAppaRao Puli // Note: Deviated from redfish privilege registry for GET & HEAD 28873946028dSAppaRao Puli // method for security reasons. 28881da66f75SEd Tanous 28897e860f15SJohn Edward Broadbent BMCWEB_ROUTE( 28907e860f15SJohn Edward Broadbent app, "/redfish/v1/Systems/system/LogServices/Crashdump/Entries/<str>/") 2891ed398213SEd Tanous // this is incorrect, should be 2892ed398213SEd Tanous // .privileges(redfish::privileges::getLogEntry) 2893432a890cSEd Tanous .privileges({{"ConfigureComponents"}}) 28947e860f15SJohn Edward Broadbent .methods(boost::beast::http::verb::get)( 289545ca1b86SEd Tanous [&app](const crow::Request& req, 28967e860f15SJohn Edward Broadbent const std::shared_ptr<bmcweb::AsyncResp>& asyncResp, 28977e860f15SJohn Edward Broadbent const std::string& param) { 28983ba00073SCarson Labrado if (!redfish::setUpRedfishRoute(app, req, asyncResp)) 289945ca1b86SEd Tanous { 290045ca1b86SEd Tanous return; 290145ca1b86SEd Tanous } 29027e860f15SJohn Edward Broadbent const std::string& logID = param; 2903e855dd28SJason M. Bills logCrashdumpEntry(asyncResp, logID, asyncResp->res.jsonValue); 29047e860f15SJohn Edward Broadbent }); 2905e855dd28SJason M. Bills } 2906e855dd28SJason M. Bills 29077e860f15SJohn Edward Broadbent inline void requestRoutesCrashdumpFile(App& app) 2908e855dd28SJason M. Bills { 29093946028dSAppaRao Puli // Note: Deviated from redfish privilege registry for GET & HEAD 29103946028dSAppaRao Puli // method for security reasons. 29117e860f15SJohn Edward Broadbent BMCWEB_ROUTE( 29127e860f15SJohn Edward Broadbent app, 29137e860f15SJohn Edward Broadbent "/redfish/v1/Systems/system/LogServices/Crashdump/Entries/<str>/<str>/") 2914ed398213SEd Tanous .privileges(redfish::privileges::getLogEntry) 29157e860f15SJohn Edward Broadbent .methods(boost::beast::http::verb::get)( 291645ca1b86SEd Tanous [&app](const crow::Request& req, 29177e860f15SJohn Edward Broadbent const std::shared_ptr<bmcweb::AsyncResp>& asyncResp, 29187e860f15SJohn Edward Broadbent const std::string& logID, const std::string& fileName) { 29192a9beeedSShounak Mitra // Do not call getRedfishRoute here since the crashdump file is not a 29202a9beeedSShounak Mitra // Redfish resource. 2921043a0536SJohnathan Mantey auto getStoredLogCallback = 2922002d39b4SEd Tanous [asyncResp, logID, fileName, url(boost::urls::url(req.urlView))]( 2923abf2add6SEd Tanous const boost::system::error_code ec, 2924002d39b4SEd Tanous const std::vector< 2925002d39b4SEd Tanous std::pair<std::string, dbus::utility::DbusVariantType>>& 29267e860f15SJohn Edward Broadbent resp) { 29271da66f75SEd Tanous if (ec) 29281da66f75SEd Tanous { 2929002d39b4SEd Tanous BMCWEB_LOG_DEBUG << "failed to get log ec: " << ec.message(); 2930f12894f8SJason M. Bills messages::internalError(asyncResp->res); 29311da66f75SEd Tanous return; 29321da66f75SEd Tanous } 2933e855dd28SJason M. Bills 2934043a0536SJohnathan Mantey std::string dbusFilename{}; 2935043a0536SJohnathan Mantey std::string dbusTimestamp{}; 2936043a0536SJohnathan Mantey std::string dbusFilepath{}; 2937043a0536SJohnathan Mantey 2938002d39b4SEd Tanous parseCrashdumpParameters(resp, dbusFilename, dbusTimestamp, 2939002d39b4SEd Tanous dbusFilepath); 2940043a0536SJohnathan Mantey 2941043a0536SJohnathan Mantey if (dbusFilename.empty() || dbusTimestamp.empty() || 2942043a0536SJohnathan Mantey dbusFilepath.empty()) 29431da66f75SEd Tanous { 2944ace85d60SEd Tanous messages::resourceMissingAtURI(asyncResp->res, url); 29451da66f75SEd Tanous return; 29461da66f75SEd Tanous } 2947e855dd28SJason M. Bills 2948043a0536SJohnathan Mantey // Verify the file name parameter is correct 2949043a0536SJohnathan Mantey if (fileName != dbusFilename) 2950043a0536SJohnathan Mantey { 2951ace85d60SEd Tanous messages::resourceMissingAtURI(asyncResp->res, url); 2952043a0536SJohnathan Mantey return; 2953043a0536SJohnathan Mantey } 2954043a0536SJohnathan Mantey 2955043a0536SJohnathan Mantey if (!std::filesystem::exists(dbusFilepath)) 2956043a0536SJohnathan Mantey { 2957ace85d60SEd Tanous messages::resourceMissingAtURI(asyncResp->res, url); 2958043a0536SJohnathan Mantey return; 2959043a0536SJohnathan Mantey } 2960002d39b4SEd Tanous std::ifstream ifs(dbusFilepath, std::ios::in | std::ios::binary); 2961002d39b4SEd Tanous asyncResp->res.body() = 2962002d39b4SEd Tanous std::string(std::istreambuf_iterator<char>{ifs}, {}); 2963043a0536SJohnathan Mantey 29647e860f15SJohn Edward Broadbent // Configure this to be a file download when accessed 29657e860f15SJohn Edward Broadbent // from a browser 2966002d39b4SEd Tanous asyncResp->res.addHeader("Content-Disposition", "attachment"); 29671da66f75SEd Tanous }; 29681da66f75SEd Tanous crow::connections::systemBus->async_method_call( 29695b61b5e8SJason M. Bills std::move(getStoredLogCallback), crashdumpObject, 29705b61b5e8SJason M. Bills crashdumpPath + std::string("/") + logID, 2971002d39b4SEd Tanous "org.freedesktop.DBus.Properties", "GetAll", crashdumpInterface); 29727e860f15SJohn Edward Broadbent }); 29731da66f75SEd Tanous } 29741da66f75SEd Tanous 2975c5a4c82aSJason M. Bills enum class OEMDiagnosticType 2976c5a4c82aSJason M. Bills { 2977c5a4c82aSJason M. Bills onDemand, 2978c5a4c82aSJason M. Bills telemetry, 2979c5a4c82aSJason M. Bills invalid, 2980c5a4c82aSJason M. Bills }; 2981c5a4c82aSJason M. Bills 2982f7725d79SEd Tanous inline OEMDiagnosticType 2983f7725d79SEd Tanous getOEMDiagnosticType(const std::string_view& oemDiagStr) 2984c5a4c82aSJason M. Bills { 2985c5a4c82aSJason M. Bills if (oemDiagStr == "OnDemand") 2986c5a4c82aSJason M. Bills { 2987c5a4c82aSJason M. Bills return OEMDiagnosticType::onDemand; 2988c5a4c82aSJason M. Bills } 2989c5a4c82aSJason M. Bills if (oemDiagStr == "Telemetry") 2990c5a4c82aSJason M. Bills { 2991c5a4c82aSJason M. Bills return OEMDiagnosticType::telemetry; 2992c5a4c82aSJason M. Bills } 2993c5a4c82aSJason M. Bills 2994c5a4c82aSJason M. Bills return OEMDiagnosticType::invalid; 2995c5a4c82aSJason M. Bills } 2996c5a4c82aSJason M. Bills 29977e860f15SJohn Edward Broadbent inline void requestRoutesCrashdumpCollect(App& app) 29981da66f75SEd Tanous { 29993946028dSAppaRao Puli // Note: Deviated from redfish privilege registry for GET & HEAD 30003946028dSAppaRao Puli // method for security reasons. 30010fda0f12SGeorge Liu BMCWEB_ROUTE( 30020fda0f12SGeorge Liu app, 30030fda0f12SGeorge Liu "/redfish/v1/Systems/system/LogServices/Crashdump/Actions/LogService.CollectDiagnosticData/") 3004ed398213SEd Tanous // The below is incorrect; Should be ConfigureManager 3005ed398213SEd Tanous //.privileges(redfish::privileges::postLogService) 3006432a890cSEd Tanous .privileges({{"ConfigureComponents"}}) 3007002d39b4SEd Tanous .methods(boost::beast::http::verb::post)( 3008002d39b4SEd Tanous [&app](const crow::Request& req, 30097e860f15SJohn Edward Broadbent const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) { 30103ba00073SCarson Labrado if (!redfish::setUpRedfishRoute(app, req, asyncResp)) 301145ca1b86SEd Tanous { 301245ca1b86SEd Tanous return; 301345ca1b86SEd Tanous } 30148e6c099aSJason M. Bills std::string diagnosticDataType; 30158e6c099aSJason M. Bills std::string oemDiagnosticDataType; 301615ed6780SWilly Tu if (!redfish::json_util::readJsonAction( 3017002d39b4SEd Tanous req, asyncResp->res, "DiagnosticDataType", diagnosticDataType, 3018002d39b4SEd Tanous "OEMDiagnosticDataType", oemDiagnosticDataType)) 30198e6c099aSJason M. Bills { 30208e6c099aSJason M. Bills return; 30218e6c099aSJason M. Bills } 30228e6c099aSJason M. Bills 30238e6c099aSJason M. Bills if (diagnosticDataType != "OEM") 30248e6c099aSJason M. Bills { 30258e6c099aSJason M. Bills BMCWEB_LOG_ERROR 30268e6c099aSJason M. Bills << "Only OEM DiagnosticDataType supported for Crashdump"; 30278e6c099aSJason M. Bills messages::actionParameterValueFormatError( 30288e6c099aSJason M. Bills asyncResp->res, diagnosticDataType, "DiagnosticDataType", 30298e6c099aSJason M. Bills "CollectDiagnosticData"); 30308e6c099aSJason M. Bills return; 30318e6c099aSJason M. Bills } 30328e6c099aSJason M. Bills 3033c5a4c82aSJason M. Bills OEMDiagnosticType oemDiagType = 3034c5a4c82aSJason M. Bills getOEMDiagnosticType(oemDiagnosticDataType); 3035c5a4c82aSJason M. Bills 3036c5a4c82aSJason M. Bills std::string iface; 3037c5a4c82aSJason M. Bills std::string method; 3038c5a4c82aSJason M. Bills std::string taskMatchStr; 3039c5a4c82aSJason M. Bills if (oemDiagType == OEMDiagnosticType::onDemand) 3040c5a4c82aSJason M. Bills { 3041c5a4c82aSJason M. Bills iface = crashdumpOnDemandInterface; 3042c5a4c82aSJason M. Bills method = "GenerateOnDemandLog"; 3043c5a4c82aSJason M. Bills taskMatchStr = "type='signal'," 3044c5a4c82aSJason M. Bills "interface='org.freedesktop.DBus.Properties'," 3045c5a4c82aSJason M. Bills "member='PropertiesChanged'," 3046c5a4c82aSJason M. Bills "arg0namespace='com.intel.crashdump'"; 3047c5a4c82aSJason M. Bills } 3048c5a4c82aSJason M. Bills else if (oemDiagType == OEMDiagnosticType::telemetry) 3049c5a4c82aSJason M. Bills { 3050c5a4c82aSJason M. Bills iface = crashdumpTelemetryInterface; 3051c5a4c82aSJason M. Bills method = "GenerateTelemetryLog"; 3052c5a4c82aSJason M. Bills taskMatchStr = "type='signal'," 3053c5a4c82aSJason M. Bills "interface='org.freedesktop.DBus.Properties'," 3054c5a4c82aSJason M. Bills "member='PropertiesChanged'," 3055c5a4c82aSJason M. Bills "arg0namespace='com.intel.crashdump'"; 3056c5a4c82aSJason M. Bills } 3057c5a4c82aSJason M. Bills else 3058c5a4c82aSJason M. Bills { 3059c5a4c82aSJason M. Bills BMCWEB_LOG_ERROR << "Unsupported OEMDiagnosticDataType: " 3060c5a4c82aSJason M. Bills << oemDiagnosticDataType; 3061c5a4c82aSJason M. Bills messages::actionParameterValueFormatError( 3062002d39b4SEd Tanous asyncResp->res, oemDiagnosticDataType, "OEMDiagnosticDataType", 3063002d39b4SEd Tanous "CollectDiagnosticData"); 3064c5a4c82aSJason M. Bills return; 3065c5a4c82aSJason M. Bills } 3066c5a4c82aSJason M. Bills 3067c5a4c82aSJason M. Bills auto collectCrashdumpCallback = 3068c5a4c82aSJason M. Bills [asyncResp, payload(task::Payload(req)), 3069c5a4c82aSJason M. Bills taskMatchStr](const boost::system::error_code ec, 307098be3e39SEd Tanous const std::string&) mutable { 30711da66f75SEd Tanous if (ec) 30721da66f75SEd Tanous { 3073002d39b4SEd Tanous if (ec.value() == boost::system::errc::operation_not_supported) 30741da66f75SEd Tanous { 3075f12894f8SJason M. Bills messages::resourceInStandby(asyncResp->res); 30761da66f75SEd Tanous } 30774363d3b2SJason M. Bills else if (ec.value() == 30784363d3b2SJason M. Bills boost::system::errc::device_or_resource_busy) 30794363d3b2SJason M. Bills { 3080002d39b4SEd Tanous messages::serviceTemporarilyUnavailable(asyncResp->res, 3081002d39b4SEd Tanous "60"); 30824363d3b2SJason M. Bills } 30831da66f75SEd Tanous else 30841da66f75SEd Tanous { 3085f12894f8SJason M. Bills messages::internalError(asyncResp->res); 30861da66f75SEd Tanous } 30871da66f75SEd Tanous return; 30881da66f75SEd Tanous } 3089002d39b4SEd Tanous std::shared_ptr<task::TaskData> task = task::TaskData::createTask( 3090*59d494eeSPatrick Williams [](boost::system::error_code err, sdbusplus::message_t&, 3091002d39b4SEd Tanous const std::shared_ptr<task::TaskData>& taskData) { 309266afe4faSJames Feist if (!err) 309366afe4faSJames Feist { 3094002d39b4SEd Tanous taskData->messages.emplace_back(messages::taskCompletedOK( 3095e5d5006bSJames Feist std::to_string(taskData->index))); 3096831d6b09SJames Feist taskData->state = "Completed"; 309766afe4faSJames Feist } 309832898ceaSJames Feist return task::completed; 309966afe4faSJames Feist }, 3100c5a4c82aSJason M. Bills taskMatchStr); 3101c5a4c82aSJason M. Bills 310246229577SJames Feist task->startTimer(std::chrono::minutes(5)); 310346229577SJames Feist task->populateResp(asyncResp->res); 310498be3e39SEd Tanous task->payload.emplace(std::move(payload)); 31051da66f75SEd Tanous }; 31068e6c099aSJason M. Bills 31071da66f75SEd Tanous crow::connections::systemBus->async_method_call( 3108002d39b4SEd Tanous std::move(collectCrashdumpCallback), crashdumpObject, crashdumpPath, 3109002d39b4SEd Tanous iface, method); 31107e860f15SJohn Edward Broadbent }); 31116eda7685SKenny L. Ku } 31126eda7685SKenny L. Ku 3113cb92c03bSAndrew Geissler /** 3114cb92c03bSAndrew Geissler * DBusLogServiceActionsClear class supports POST method for ClearLog action. 3115cb92c03bSAndrew Geissler */ 31167e860f15SJohn Edward Broadbent inline void requestRoutesDBusLogServiceActionsClear(App& app) 3117cb92c03bSAndrew Geissler { 3118cb92c03bSAndrew Geissler /** 3119cb92c03bSAndrew Geissler * Function handles POST method request. 3120cb92c03bSAndrew Geissler * The Clear Log actions does not require any parameter.The action deletes 3121cb92c03bSAndrew Geissler * all entries found in the Entries collection for this Log Service. 3122cb92c03bSAndrew Geissler */ 31237e860f15SJohn Edward Broadbent 31240fda0f12SGeorge Liu BMCWEB_ROUTE( 31250fda0f12SGeorge Liu app, 31260fda0f12SGeorge Liu "/redfish/v1/Systems/system/LogServices/EventLog/Actions/LogService.ClearLog/") 3127ed398213SEd Tanous .privileges(redfish::privileges::postLogService) 31287e860f15SJohn Edward Broadbent .methods(boost::beast::http::verb::post)( 312945ca1b86SEd Tanous [&app](const crow::Request& req, 31307e860f15SJohn Edward Broadbent const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) { 31313ba00073SCarson Labrado if (!redfish::setUpRedfishRoute(app, req, asyncResp)) 313245ca1b86SEd Tanous { 313345ca1b86SEd Tanous return; 313445ca1b86SEd Tanous } 3135cb92c03bSAndrew Geissler BMCWEB_LOG_DEBUG << "Do delete all entries."; 3136cb92c03bSAndrew Geissler 3137cb92c03bSAndrew Geissler // Process response from Logging service. 3138002d39b4SEd Tanous auto respHandler = [asyncResp](const boost::system::error_code ec) { 3139002d39b4SEd Tanous BMCWEB_LOG_DEBUG << "doClearLog resp_handler callback: Done"; 3140cb92c03bSAndrew Geissler if (ec) 3141cb92c03bSAndrew Geissler { 3142cb92c03bSAndrew Geissler // TODO Handle for specific error code 3143002d39b4SEd Tanous BMCWEB_LOG_ERROR << "doClearLog resp_handler got error " << ec; 3144cb92c03bSAndrew Geissler asyncResp->res.result( 3145cb92c03bSAndrew Geissler boost::beast::http::status::internal_server_error); 3146cb92c03bSAndrew Geissler return; 3147cb92c03bSAndrew Geissler } 3148cb92c03bSAndrew Geissler 3149002d39b4SEd Tanous asyncResp->res.result(boost::beast::http::status::no_content); 3150cb92c03bSAndrew Geissler }; 3151cb92c03bSAndrew Geissler 3152cb92c03bSAndrew Geissler // Make call to Logging service to request Clear Log 3153cb92c03bSAndrew Geissler crow::connections::systemBus->async_method_call( 31542c70f800SEd Tanous respHandler, "xyz.openbmc_project.Logging", 3155cb92c03bSAndrew Geissler "/xyz/openbmc_project/logging", 3156cb92c03bSAndrew Geissler "xyz.openbmc_project.Collection.DeleteAll", "DeleteAll"); 31577e860f15SJohn Edward Broadbent }); 3158cb92c03bSAndrew Geissler } 3159a3316fc6SZhikuiRen 3160a3316fc6SZhikuiRen /**************************************************** 3161a3316fc6SZhikuiRen * Redfish PostCode interfaces 3162a3316fc6SZhikuiRen * using DBUS interface: getPostCodesTS 3163a3316fc6SZhikuiRen ******************************************************/ 31647e860f15SJohn Edward Broadbent inline void requestRoutesPostCodesLogService(App& app) 3165a3316fc6SZhikuiRen { 31667e860f15SJohn Edward Broadbent BMCWEB_ROUTE(app, "/redfish/v1/Systems/system/LogServices/PostCodes/") 3167ed398213SEd Tanous .privileges(redfish::privileges::getLogService) 3168002d39b4SEd Tanous .methods(boost::beast::http::verb::get)( 3169002d39b4SEd Tanous [&app](const crow::Request& req, 3170002d39b4SEd Tanous const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) { 31713ba00073SCarson Labrado if (!redfish::setUpRedfishRoute(app, req, asyncResp)) 317245ca1b86SEd Tanous { 317345ca1b86SEd Tanous return; 317445ca1b86SEd Tanous } 31751476687dSEd Tanous 31761476687dSEd Tanous asyncResp->res.jsonValue["@odata.id"] = 31771476687dSEd Tanous "/redfish/v1/Systems/system/LogServices/PostCodes"; 31781476687dSEd Tanous asyncResp->res.jsonValue["@odata.type"] = 31791476687dSEd Tanous "#LogService.v1_1_0.LogService"; 31801476687dSEd Tanous asyncResp->res.jsonValue["Name"] = "POST Code Log Service"; 31811476687dSEd Tanous asyncResp->res.jsonValue["Description"] = "POST Code Log Service"; 31821476687dSEd Tanous asyncResp->res.jsonValue["Id"] = "BIOS POST Code Log"; 31831476687dSEd Tanous asyncResp->res.jsonValue["OverWritePolicy"] = "WrapsWhenFull"; 31841476687dSEd Tanous asyncResp->res.jsonValue["Entries"]["@odata.id"] = 31851476687dSEd Tanous "/redfish/v1/Systems/system/LogServices/PostCodes/Entries"; 31867c8c4058STejas Patil 31877c8c4058STejas Patil std::pair<std::string, std::string> redfishDateTimeOffset = 31887c8c4058STejas Patil crow::utility::getDateTimeOffsetNow(); 31890fda0f12SGeorge Liu asyncResp->res.jsonValue["DateTime"] = redfishDateTimeOffset.first; 31907c8c4058STejas Patil asyncResp->res.jsonValue["DateTimeLocalOffset"] = 31917c8c4058STejas Patil redfishDateTimeOffset.second; 31927c8c4058STejas Patil 3193a3316fc6SZhikuiRen asyncResp->res.jsonValue["Actions"]["#LogService.ClearLog"] = { 31947e860f15SJohn Edward Broadbent {"target", 31950fda0f12SGeorge Liu "/redfish/v1/Systems/system/LogServices/PostCodes/Actions/LogService.ClearLog"}}; 31967e860f15SJohn Edward Broadbent }); 3197a3316fc6SZhikuiRen } 3198a3316fc6SZhikuiRen 31997e860f15SJohn Edward Broadbent inline void requestRoutesPostCodesClear(App& app) 3200a3316fc6SZhikuiRen { 32010fda0f12SGeorge Liu BMCWEB_ROUTE( 32020fda0f12SGeorge Liu app, 32030fda0f12SGeorge Liu "/redfish/v1/Systems/system/LogServices/PostCodes/Actions/LogService.ClearLog/") 3204ed398213SEd Tanous // The following privilege is incorrect; It should be ConfigureManager 3205ed398213SEd Tanous //.privileges(redfish::privileges::postLogService) 3206432a890cSEd Tanous .privileges({{"ConfigureComponents"}}) 32077e860f15SJohn Edward Broadbent .methods(boost::beast::http::verb::post)( 320845ca1b86SEd Tanous [&app](const crow::Request& req, 32097e860f15SJohn Edward Broadbent const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) { 32103ba00073SCarson Labrado if (!redfish::setUpRedfishRoute(app, req, asyncResp)) 321145ca1b86SEd Tanous { 321245ca1b86SEd Tanous return; 321345ca1b86SEd Tanous } 3214a3316fc6SZhikuiRen BMCWEB_LOG_DEBUG << "Do delete all postcodes entries."; 3215a3316fc6SZhikuiRen 3216a3316fc6SZhikuiRen // Make call to post-code service to request clear all 3217a3316fc6SZhikuiRen crow::connections::systemBus->async_method_call( 3218a3316fc6SZhikuiRen [asyncResp](const boost::system::error_code ec) { 3219a3316fc6SZhikuiRen if (ec) 3220a3316fc6SZhikuiRen { 3221a3316fc6SZhikuiRen // TODO Handle for specific error code 3222002d39b4SEd Tanous BMCWEB_LOG_ERROR << "doClearPostCodes resp_handler got error " 32237e860f15SJohn Edward Broadbent << ec; 3224002d39b4SEd Tanous asyncResp->res.result( 3225002d39b4SEd Tanous boost::beast::http::status::internal_server_error); 3226a3316fc6SZhikuiRen messages::internalError(asyncResp->res); 3227a3316fc6SZhikuiRen return; 3228a3316fc6SZhikuiRen } 3229a3316fc6SZhikuiRen }, 323015124765SJonathan Doman "xyz.openbmc_project.State.Boot.PostCode0", 323115124765SJonathan Doman "/xyz/openbmc_project/State/Boot/PostCode0", 3232a3316fc6SZhikuiRen "xyz.openbmc_project.Collection.DeleteAll", "DeleteAll"); 32337e860f15SJohn Edward Broadbent }); 3234a3316fc6SZhikuiRen } 3235a3316fc6SZhikuiRen 3236a3316fc6SZhikuiRen static void fillPostCodeEntry( 32378d1b46d7Szhanghch05 const std::shared_ptr<bmcweb::AsyncResp>& aResp, 32386c9a279eSManojkiran Eda const boost::container::flat_map< 32396c9a279eSManojkiran Eda uint64_t, std::tuple<uint64_t, std::vector<uint8_t>>>& postcode, 3240a3316fc6SZhikuiRen const uint16_t bootIndex, const uint64_t codeIndex = 0, 3241a3316fc6SZhikuiRen const uint64_t skip = 0, const uint64_t top = 0) 3242a3316fc6SZhikuiRen { 3243a3316fc6SZhikuiRen // Get the Message from the MessageRegistry 3244fffb8c1fSEd Tanous const registries::Message* message = 3245fffb8c1fSEd Tanous registries::getMessage("OpenBMC.0.2.BIOSPOSTCode"); 3246a3316fc6SZhikuiRen 3247a3316fc6SZhikuiRen uint64_t currentCodeIndex = 0; 3248a3316fc6SZhikuiRen nlohmann::json& logEntryArray = aResp->res.jsonValue["Members"]; 3249a3316fc6SZhikuiRen 3250a3316fc6SZhikuiRen uint64_t firstCodeTimeUs = 0; 32516c9a279eSManojkiran Eda for (const std::pair<uint64_t, std::tuple<uint64_t, std::vector<uint8_t>>>& 32526c9a279eSManojkiran Eda code : postcode) 3253a3316fc6SZhikuiRen { 3254a3316fc6SZhikuiRen currentCodeIndex++; 3255a3316fc6SZhikuiRen std::string postcodeEntryID = 3256a3316fc6SZhikuiRen "B" + std::to_string(bootIndex) + "-" + 3257a3316fc6SZhikuiRen std::to_string(currentCodeIndex); // 1 based index in EntryID string 3258a3316fc6SZhikuiRen 3259a3316fc6SZhikuiRen uint64_t usecSinceEpoch = code.first; 3260a3316fc6SZhikuiRen uint64_t usTimeOffset = 0; 3261a3316fc6SZhikuiRen 3262a3316fc6SZhikuiRen if (1 == currentCodeIndex) 3263a3316fc6SZhikuiRen { // already incremented 3264a3316fc6SZhikuiRen firstCodeTimeUs = code.first; 3265a3316fc6SZhikuiRen } 3266a3316fc6SZhikuiRen else 3267a3316fc6SZhikuiRen { 3268a3316fc6SZhikuiRen usTimeOffset = code.first - firstCodeTimeUs; 3269a3316fc6SZhikuiRen } 3270a3316fc6SZhikuiRen 3271a3316fc6SZhikuiRen // skip if no specific codeIndex is specified and currentCodeIndex does 3272a3316fc6SZhikuiRen // not fall between top and skip 3273a3316fc6SZhikuiRen if ((codeIndex == 0) && 3274a3316fc6SZhikuiRen (currentCodeIndex <= skip || currentCodeIndex > top)) 3275a3316fc6SZhikuiRen { 3276a3316fc6SZhikuiRen continue; 3277a3316fc6SZhikuiRen } 3278a3316fc6SZhikuiRen 32794e0453b1SGunnar Mills // skip if a specific codeIndex is specified and does not match the 3280a3316fc6SZhikuiRen // currentIndex 3281a3316fc6SZhikuiRen if ((codeIndex > 0) && (currentCodeIndex != codeIndex)) 3282a3316fc6SZhikuiRen { 3283a3316fc6SZhikuiRen // This is done for simplicity. 1st entry is needed to calculate 3284a3316fc6SZhikuiRen // time offset. To improve efficiency, one can get to the entry 3285a3316fc6SZhikuiRen // directly (possibly with flatmap's nth method) 3286a3316fc6SZhikuiRen continue; 3287a3316fc6SZhikuiRen } 3288a3316fc6SZhikuiRen 3289a3316fc6SZhikuiRen // currentCodeIndex is within top and skip or equal to specified code 3290a3316fc6SZhikuiRen // index 3291a3316fc6SZhikuiRen 3292a3316fc6SZhikuiRen // Get the Created time from the timestamp 3293a3316fc6SZhikuiRen std::string entryTimeStr; 32941d8782e7SNan Zhou entryTimeStr = 32951d8782e7SNan Zhou crow::utility::getDateTimeUint(usecSinceEpoch / 1000 / 1000); 3296a3316fc6SZhikuiRen 3297a3316fc6SZhikuiRen // assemble messageArgs: BootIndex, TimeOffset(100us), PostCode(hex) 3298a3316fc6SZhikuiRen std::ostringstream hexCode; 3299a3316fc6SZhikuiRen hexCode << "0x" << std::setfill('0') << std::setw(2) << std::hex 33006c9a279eSManojkiran Eda << std::get<0>(code.second); 3301a3316fc6SZhikuiRen std::ostringstream timeOffsetStr; 3302a3316fc6SZhikuiRen // Set Fixed -Point Notation 3303a3316fc6SZhikuiRen timeOffsetStr << std::fixed; 3304a3316fc6SZhikuiRen // Set precision to 4 digits 3305a3316fc6SZhikuiRen timeOffsetStr << std::setprecision(4); 3306a3316fc6SZhikuiRen // Add double to stream 3307a3316fc6SZhikuiRen timeOffsetStr << static_cast<double>(usTimeOffset) / 1000 / 1000; 3308a3316fc6SZhikuiRen std::vector<std::string> messageArgs = { 3309a3316fc6SZhikuiRen std::to_string(bootIndex), timeOffsetStr.str(), hexCode.str()}; 3310a3316fc6SZhikuiRen 3311a3316fc6SZhikuiRen // Get MessageArgs template from message registry 3312a3316fc6SZhikuiRen std::string msg; 3313a3316fc6SZhikuiRen if (message != nullptr) 3314a3316fc6SZhikuiRen { 3315a3316fc6SZhikuiRen msg = message->message; 3316a3316fc6SZhikuiRen 3317a3316fc6SZhikuiRen // fill in this post code value 3318a3316fc6SZhikuiRen int i = 0; 3319a3316fc6SZhikuiRen for (const std::string& messageArg : messageArgs) 3320a3316fc6SZhikuiRen { 3321a3316fc6SZhikuiRen std::string argStr = "%" + std::to_string(++i); 3322a3316fc6SZhikuiRen size_t argPos = msg.find(argStr); 3323a3316fc6SZhikuiRen if (argPos != std::string::npos) 3324a3316fc6SZhikuiRen { 3325a3316fc6SZhikuiRen msg.replace(argPos, argStr.length(), messageArg); 3326a3316fc6SZhikuiRen } 3327a3316fc6SZhikuiRen } 3328a3316fc6SZhikuiRen } 3329a3316fc6SZhikuiRen 3330d4342a92STim Lee // Get Severity template from message registry 3331d4342a92STim Lee std::string severity; 3332d4342a92STim Lee if (message != nullptr) 3333d4342a92STim Lee { 33345f2b84eeSEd Tanous severity = message->messageSeverity; 3335d4342a92STim Lee } 3336d4342a92STim Lee 3337a3316fc6SZhikuiRen // add to AsyncResp 3338a3316fc6SZhikuiRen logEntryArray.push_back({}); 3339a3316fc6SZhikuiRen nlohmann::json& bmcLogEntry = logEntryArray.back(); 334084afc48bSJason M. Bills bmcLogEntry["@odata.type"] = "#LogEntry.v1_8_0.LogEntry"; 334184afc48bSJason M. Bills bmcLogEntry["@odata.id"] = 33420fda0f12SGeorge Liu "/redfish/v1/Systems/system/LogServices/PostCodes/Entries/" + 334384afc48bSJason M. Bills postcodeEntryID; 334484afc48bSJason M. Bills bmcLogEntry["Name"] = "POST Code Log Entry"; 334584afc48bSJason M. Bills bmcLogEntry["Id"] = postcodeEntryID; 334684afc48bSJason M. Bills bmcLogEntry["Message"] = std::move(msg); 334784afc48bSJason M. Bills bmcLogEntry["MessageId"] = "OpenBMC.0.2.BIOSPOSTCode"; 334884afc48bSJason M. Bills bmcLogEntry["MessageArgs"] = std::move(messageArgs); 334984afc48bSJason M. Bills bmcLogEntry["EntryType"] = "Event"; 335084afc48bSJason M. Bills bmcLogEntry["Severity"] = std::move(severity); 335184afc48bSJason M. Bills bmcLogEntry["Created"] = entryTimeStr; 3352647b3cdcSGeorge Liu if (!std::get<std::vector<uint8_t>>(code.second).empty()) 3353647b3cdcSGeorge Liu { 3354647b3cdcSGeorge Liu bmcLogEntry["AdditionalDataURI"] = 3355647b3cdcSGeorge Liu "/redfish/v1/Systems/system/LogServices/PostCodes/Entries/" + 3356647b3cdcSGeorge Liu postcodeEntryID + "/attachment"; 3357647b3cdcSGeorge Liu } 3358a3316fc6SZhikuiRen } 3359a3316fc6SZhikuiRen } 3360a3316fc6SZhikuiRen 33618d1b46d7Szhanghch05 static void getPostCodeForEntry(const std::shared_ptr<bmcweb::AsyncResp>& aResp, 3362a3316fc6SZhikuiRen const uint16_t bootIndex, 3363a3316fc6SZhikuiRen const uint64_t codeIndex) 3364a3316fc6SZhikuiRen { 3365a3316fc6SZhikuiRen crow::connections::systemBus->async_method_call( 33666c9a279eSManojkiran Eda [aResp, bootIndex, 33676c9a279eSManojkiran Eda codeIndex](const boost::system::error_code ec, 33686c9a279eSManojkiran Eda const boost::container::flat_map< 33696c9a279eSManojkiran Eda uint64_t, std::tuple<uint64_t, std::vector<uint8_t>>>& 33706c9a279eSManojkiran Eda postcode) { 3371a3316fc6SZhikuiRen if (ec) 3372a3316fc6SZhikuiRen { 3373a3316fc6SZhikuiRen BMCWEB_LOG_DEBUG << "DBUS POST CODE PostCode response error"; 3374a3316fc6SZhikuiRen messages::internalError(aResp->res); 3375a3316fc6SZhikuiRen return; 3376a3316fc6SZhikuiRen } 3377a3316fc6SZhikuiRen 3378a3316fc6SZhikuiRen // skip the empty postcode boots 3379a3316fc6SZhikuiRen if (postcode.empty()) 3380a3316fc6SZhikuiRen { 3381a3316fc6SZhikuiRen return; 3382a3316fc6SZhikuiRen } 3383a3316fc6SZhikuiRen 3384a3316fc6SZhikuiRen fillPostCodeEntry(aResp, postcode, bootIndex, codeIndex); 3385a3316fc6SZhikuiRen 3386a3316fc6SZhikuiRen aResp->res.jsonValue["Members@odata.count"] = 3387a3316fc6SZhikuiRen aResp->res.jsonValue["Members"].size(); 3388a3316fc6SZhikuiRen }, 338915124765SJonathan Doman "xyz.openbmc_project.State.Boot.PostCode0", 339015124765SJonathan Doman "/xyz/openbmc_project/State/Boot/PostCode0", 3391a3316fc6SZhikuiRen "xyz.openbmc_project.State.Boot.PostCode", "GetPostCodesWithTimeStamp", 3392a3316fc6SZhikuiRen bootIndex); 3393a3316fc6SZhikuiRen } 3394a3316fc6SZhikuiRen 33958d1b46d7Szhanghch05 static void getPostCodeForBoot(const std::shared_ptr<bmcweb::AsyncResp>& aResp, 3396a3316fc6SZhikuiRen const uint16_t bootIndex, 3397a3316fc6SZhikuiRen const uint16_t bootCount, 3398a3316fc6SZhikuiRen const uint64_t entryCount, const uint64_t skip, 3399a3316fc6SZhikuiRen const uint64_t top) 3400a3316fc6SZhikuiRen { 3401a3316fc6SZhikuiRen crow::connections::systemBus->async_method_call( 3402a3316fc6SZhikuiRen [aResp, bootIndex, bootCount, entryCount, skip, 3403a3316fc6SZhikuiRen top](const boost::system::error_code ec, 34046c9a279eSManojkiran Eda const boost::container::flat_map< 34056c9a279eSManojkiran Eda uint64_t, std::tuple<uint64_t, std::vector<uint8_t>>>& 34066c9a279eSManojkiran Eda postcode) { 3407a3316fc6SZhikuiRen if (ec) 3408a3316fc6SZhikuiRen { 3409a3316fc6SZhikuiRen BMCWEB_LOG_DEBUG << "DBUS POST CODE PostCode response error"; 3410a3316fc6SZhikuiRen messages::internalError(aResp->res); 3411a3316fc6SZhikuiRen return; 3412a3316fc6SZhikuiRen } 3413a3316fc6SZhikuiRen 3414a3316fc6SZhikuiRen uint64_t endCount = entryCount; 3415a3316fc6SZhikuiRen if (!postcode.empty()) 3416a3316fc6SZhikuiRen { 3417a3316fc6SZhikuiRen endCount = entryCount + postcode.size(); 3418a3316fc6SZhikuiRen 3419a3316fc6SZhikuiRen if ((skip < endCount) && ((top + skip) > entryCount)) 3420a3316fc6SZhikuiRen { 3421002d39b4SEd Tanous uint64_t thisBootSkip = std::max(skip, entryCount) - entryCount; 3422a3316fc6SZhikuiRen uint64_t thisBootTop = 3423a3316fc6SZhikuiRen std::min(top + skip, endCount) - entryCount; 3424a3316fc6SZhikuiRen 3425002d39b4SEd Tanous fillPostCodeEntry(aResp, postcode, bootIndex, 0, thisBootSkip, 3426002d39b4SEd Tanous thisBootTop); 3427a3316fc6SZhikuiRen } 3428a3316fc6SZhikuiRen aResp->res.jsonValue["Members@odata.count"] = endCount; 3429a3316fc6SZhikuiRen } 3430a3316fc6SZhikuiRen 3431a3316fc6SZhikuiRen // continue to previous bootIndex 3432a3316fc6SZhikuiRen if (bootIndex < bootCount) 3433a3316fc6SZhikuiRen { 3434a3316fc6SZhikuiRen getPostCodeForBoot(aResp, static_cast<uint16_t>(bootIndex + 1), 3435a3316fc6SZhikuiRen bootCount, endCount, skip, top); 3436a3316fc6SZhikuiRen } 3437a3316fc6SZhikuiRen else 3438a3316fc6SZhikuiRen { 3439a3316fc6SZhikuiRen aResp->res.jsonValue["Members@odata.nextLink"] = 34400fda0f12SGeorge Liu "/redfish/v1/Systems/system/LogServices/PostCodes/Entries?$skip=" + 3441a3316fc6SZhikuiRen std::to_string(skip + top); 3442a3316fc6SZhikuiRen } 3443a3316fc6SZhikuiRen }, 344415124765SJonathan Doman "xyz.openbmc_project.State.Boot.PostCode0", 344515124765SJonathan Doman "/xyz/openbmc_project/State/Boot/PostCode0", 3446a3316fc6SZhikuiRen "xyz.openbmc_project.State.Boot.PostCode", "GetPostCodesWithTimeStamp", 3447a3316fc6SZhikuiRen bootIndex); 3448a3316fc6SZhikuiRen } 3449a3316fc6SZhikuiRen 34508d1b46d7Szhanghch05 static void 34518d1b46d7Szhanghch05 getCurrentBootNumber(const std::shared_ptr<bmcweb::AsyncResp>& aResp, 3452a3316fc6SZhikuiRen const uint64_t skip, const uint64_t top) 3453a3316fc6SZhikuiRen { 3454a3316fc6SZhikuiRen uint64_t entryCount = 0; 34551e1e598dSJonathan Doman sdbusplus::asio::getProperty<uint16_t>( 34561e1e598dSJonathan Doman *crow::connections::systemBus, 34571e1e598dSJonathan Doman "xyz.openbmc_project.State.Boot.PostCode0", 34581e1e598dSJonathan Doman "/xyz/openbmc_project/State/Boot/PostCode0", 34591e1e598dSJonathan Doman "xyz.openbmc_project.State.Boot.PostCode", "CurrentBootCycleCount", 34601e1e598dSJonathan Doman [aResp, entryCount, skip, top](const boost::system::error_code ec, 34611e1e598dSJonathan Doman const uint16_t bootCount) { 3462a3316fc6SZhikuiRen if (ec) 3463a3316fc6SZhikuiRen { 3464a3316fc6SZhikuiRen BMCWEB_LOG_DEBUG << "DBUS response error " << ec; 3465a3316fc6SZhikuiRen messages::internalError(aResp->res); 3466a3316fc6SZhikuiRen return; 3467a3316fc6SZhikuiRen } 34681e1e598dSJonathan Doman getPostCodeForBoot(aResp, 1, bootCount, entryCount, skip, top); 34691e1e598dSJonathan Doman }); 3470a3316fc6SZhikuiRen } 3471a3316fc6SZhikuiRen 34727e860f15SJohn Edward Broadbent inline void requestRoutesPostCodesEntryCollection(App& app) 3473a3316fc6SZhikuiRen { 34747e860f15SJohn Edward Broadbent BMCWEB_ROUTE(app, 34757e860f15SJohn Edward Broadbent "/redfish/v1/Systems/system/LogServices/PostCodes/Entries/") 3476ed398213SEd Tanous .privileges(redfish::privileges::getLogEntryCollection) 34777e860f15SJohn Edward Broadbent .methods(boost::beast::http::verb::get)( 347845ca1b86SEd Tanous [&app](const crow::Request& req, 34797e860f15SJohn Edward Broadbent const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) { 3480c937d2bfSEd Tanous query_param::QueryCapabilities capabilities = { 3481c937d2bfSEd Tanous .canDelegateTop = true, 3482c937d2bfSEd Tanous .canDelegateSkip = true, 3483c937d2bfSEd Tanous }; 3484c937d2bfSEd Tanous query_param::Query delegatedQuery; 3485c937d2bfSEd Tanous if (!redfish::setUpRedfishRouteWithDelegation( 34863ba00073SCarson Labrado app, req, asyncResp, delegatedQuery, capabilities)) 348745ca1b86SEd Tanous { 348845ca1b86SEd Tanous return; 348945ca1b86SEd Tanous } 3490a3316fc6SZhikuiRen asyncResp->res.jsonValue["@odata.type"] = 3491a3316fc6SZhikuiRen "#LogEntryCollection.LogEntryCollection"; 3492a3316fc6SZhikuiRen asyncResp->res.jsonValue["@odata.id"] = 3493a3316fc6SZhikuiRen "/redfish/v1/Systems/system/LogServices/PostCodes/Entries"; 3494a3316fc6SZhikuiRen asyncResp->res.jsonValue["Name"] = "BIOS POST Code Log Entries"; 3495a3316fc6SZhikuiRen asyncResp->res.jsonValue["Description"] = 3496a3316fc6SZhikuiRen "Collection of POST Code Log Entries"; 3497a3316fc6SZhikuiRen asyncResp->res.jsonValue["Members"] = nlohmann::json::array(); 3498a3316fc6SZhikuiRen asyncResp->res.jsonValue["Members@odata.count"] = 0; 3499a3316fc6SZhikuiRen 3500c937d2bfSEd Tanous getCurrentBootNumber(asyncResp, delegatedQuery.skip, 3501c937d2bfSEd Tanous delegatedQuery.top); 35027e860f15SJohn Edward Broadbent }); 3503a3316fc6SZhikuiRen } 3504a3316fc6SZhikuiRen 3505647b3cdcSGeorge Liu /** 3506647b3cdcSGeorge Liu * @brief Parse post code ID and get the current value and index value 3507647b3cdcSGeorge Liu * eg: postCodeID=B1-2, currentValue=1, index=2 3508647b3cdcSGeorge Liu * 3509647b3cdcSGeorge Liu * @param[in] postCodeID Post Code ID 3510647b3cdcSGeorge Liu * @param[out] currentValue Current value 3511647b3cdcSGeorge Liu * @param[out] index Index value 3512647b3cdcSGeorge Liu * 3513647b3cdcSGeorge Liu * @return bool true if the parsing is successful, false the parsing fails 3514647b3cdcSGeorge Liu */ 3515647b3cdcSGeorge Liu inline static bool parsePostCode(const std::string& postCodeID, 3516647b3cdcSGeorge Liu uint64_t& currentValue, uint16_t& index) 3517647b3cdcSGeorge Liu { 3518647b3cdcSGeorge Liu std::vector<std::string> split; 3519647b3cdcSGeorge Liu boost::algorithm::split(split, postCodeID, boost::is_any_of("-")); 3520647b3cdcSGeorge Liu if (split.size() != 2 || split[0].length() < 2 || split[0].front() != 'B') 3521647b3cdcSGeorge Liu { 3522647b3cdcSGeorge Liu return false; 3523647b3cdcSGeorge Liu } 3524647b3cdcSGeorge Liu 3525ca45aa3cSEd Tanous // NOLINTNEXTLINE(cppcoreguidelines-pro-bounds-pointer-arithmetic) 3526647b3cdcSGeorge Liu const char* start = split[0].data() + 1; 3527ca45aa3cSEd Tanous // NOLINTNEXTLINE(cppcoreguidelines-pro-bounds-pointer-arithmetic) 3528647b3cdcSGeorge Liu const char* end = split[0].data() + split[0].size(); 3529647b3cdcSGeorge Liu auto [ptrIndex, ecIndex] = std::from_chars(start, end, index); 3530647b3cdcSGeorge Liu 3531647b3cdcSGeorge Liu if (ptrIndex != end || ecIndex != std::errc()) 3532647b3cdcSGeorge Liu { 3533647b3cdcSGeorge Liu return false; 3534647b3cdcSGeorge Liu } 3535647b3cdcSGeorge Liu 3536647b3cdcSGeorge Liu start = split[1].data(); 3537ca45aa3cSEd Tanous 3538ca45aa3cSEd Tanous // NOLINTNEXTLINE(cppcoreguidelines-pro-bounds-pointer-arithmetic) 3539647b3cdcSGeorge Liu end = split[1].data() + split[1].size(); 3540647b3cdcSGeorge Liu auto [ptrValue, ecValue] = std::from_chars(start, end, currentValue); 3541647b3cdcSGeorge Liu 3542517d9a58STony Lee return ptrValue == end && ecValue == std::errc(); 3543647b3cdcSGeorge Liu } 3544647b3cdcSGeorge Liu 3545647b3cdcSGeorge Liu inline void requestRoutesPostCodesEntryAdditionalData(App& app) 3546647b3cdcSGeorge Liu { 35470fda0f12SGeorge Liu BMCWEB_ROUTE( 35480fda0f12SGeorge Liu app, 35490fda0f12SGeorge Liu "/redfish/v1/Systems/system/LogServices/PostCodes/Entries/<str>/attachment/") 3550647b3cdcSGeorge Liu .privileges(redfish::privileges::getLogEntry) 3551647b3cdcSGeorge Liu .methods(boost::beast::http::verb::get)( 355245ca1b86SEd Tanous [&app](const crow::Request& req, 3553647b3cdcSGeorge Liu const std::shared_ptr<bmcweb::AsyncResp>& asyncResp, 3554647b3cdcSGeorge Liu const std::string& postCodeID) { 35553ba00073SCarson Labrado if (!redfish::setUpRedfishRoute(app, req, asyncResp)) 355645ca1b86SEd Tanous { 355745ca1b86SEd Tanous return; 355845ca1b86SEd Tanous } 3559002d39b4SEd Tanous if (!http_helpers::isOctetAccepted(req.getHeaderValue("Accept"))) 3560647b3cdcSGeorge Liu { 3561002d39b4SEd Tanous asyncResp->res.result(boost::beast::http::status::bad_request); 3562647b3cdcSGeorge Liu return; 3563647b3cdcSGeorge Liu } 3564647b3cdcSGeorge Liu 3565647b3cdcSGeorge Liu uint64_t currentValue = 0; 3566647b3cdcSGeorge Liu uint16_t index = 0; 3567647b3cdcSGeorge Liu if (!parsePostCode(postCodeID, currentValue, index)) 3568647b3cdcSGeorge Liu { 3569002d39b4SEd Tanous messages::resourceNotFound(asyncResp->res, "LogEntry", postCodeID); 3570647b3cdcSGeorge Liu return; 3571647b3cdcSGeorge Liu } 3572647b3cdcSGeorge Liu 3573647b3cdcSGeorge Liu crow::connections::systemBus->async_method_call( 3574647b3cdcSGeorge Liu [asyncResp, postCodeID, currentValue]( 3575647b3cdcSGeorge Liu const boost::system::error_code ec, 3576002d39b4SEd Tanous const std::vector<std::tuple<uint64_t, std::vector<uint8_t>>>& 3577002d39b4SEd Tanous postcodes) { 3578647b3cdcSGeorge Liu if (ec.value() == EBADR) 3579647b3cdcSGeorge Liu { 3580002d39b4SEd Tanous messages::resourceNotFound(asyncResp->res, "LogEntry", 3581002d39b4SEd Tanous postCodeID); 3582647b3cdcSGeorge Liu return; 3583647b3cdcSGeorge Liu } 3584647b3cdcSGeorge Liu if (ec) 3585647b3cdcSGeorge Liu { 3586647b3cdcSGeorge Liu BMCWEB_LOG_DEBUG << "DBUS response error " << ec; 3587647b3cdcSGeorge Liu messages::internalError(asyncResp->res); 3588647b3cdcSGeorge Liu return; 3589647b3cdcSGeorge Liu } 3590647b3cdcSGeorge Liu 3591647b3cdcSGeorge Liu size_t value = static_cast<size_t>(currentValue) - 1; 3592002d39b4SEd Tanous if (value == std::string::npos || postcodes.size() < currentValue) 3593647b3cdcSGeorge Liu { 3594647b3cdcSGeorge Liu BMCWEB_LOG_ERROR << "Wrong currentValue value"; 3595002d39b4SEd Tanous messages::resourceNotFound(asyncResp->res, "LogEntry", 3596002d39b4SEd Tanous postCodeID); 3597647b3cdcSGeorge Liu return; 3598647b3cdcSGeorge Liu } 3599647b3cdcSGeorge Liu 36009eb808c1SEd Tanous const auto& [tID, c] = postcodes[value]; 360146ff87baSEd Tanous if (c.empty()) 3602647b3cdcSGeorge Liu { 3603647b3cdcSGeorge Liu BMCWEB_LOG_INFO << "No found post code data"; 3604002d39b4SEd Tanous messages::resourceNotFound(asyncResp->res, "LogEntry", 3605002d39b4SEd Tanous postCodeID); 3606647b3cdcSGeorge Liu return; 3607647b3cdcSGeorge Liu } 360846ff87baSEd Tanous // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast) 360946ff87baSEd Tanous const char* d = reinterpret_cast<const char*>(c.data()); 361046ff87baSEd Tanous std::string_view strData(d, c.size()); 3611647b3cdcSGeorge Liu 3612647b3cdcSGeorge Liu asyncResp->res.addHeader("Content-Type", 3613647b3cdcSGeorge Liu "application/octet-stream"); 3614002d39b4SEd Tanous asyncResp->res.addHeader("Content-Transfer-Encoding", "Base64"); 3615002d39b4SEd Tanous asyncResp->res.body() = crow::utility::base64encode(strData); 3616647b3cdcSGeorge Liu }, 3617647b3cdcSGeorge Liu "xyz.openbmc_project.State.Boot.PostCode0", 3618647b3cdcSGeorge Liu "/xyz/openbmc_project/State/Boot/PostCode0", 3619002d39b4SEd Tanous "xyz.openbmc_project.State.Boot.PostCode", "GetPostCodes", index); 3620647b3cdcSGeorge Liu }); 3621647b3cdcSGeorge Liu } 3622647b3cdcSGeorge Liu 36237e860f15SJohn Edward Broadbent inline void requestRoutesPostCodesEntry(App& app) 3624a3316fc6SZhikuiRen { 36257e860f15SJohn Edward Broadbent BMCWEB_ROUTE( 36267e860f15SJohn Edward Broadbent app, "/redfish/v1/Systems/system/LogServices/PostCodes/Entries/<str>/") 3627ed398213SEd Tanous .privileges(redfish::privileges::getLogEntry) 36287e860f15SJohn Edward Broadbent .methods(boost::beast::http::verb::get)( 362945ca1b86SEd Tanous [&app](const crow::Request& req, 36307e860f15SJohn Edward Broadbent const std::shared_ptr<bmcweb::AsyncResp>& asyncResp, 36317e860f15SJohn Edward Broadbent const std::string& targetID) { 36323ba00073SCarson Labrado if (!redfish::setUpRedfishRoute(app, req, asyncResp)) 363345ca1b86SEd Tanous { 363445ca1b86SEd Tanous return; 363545ca1b86SEd Tanous } 3636647b3cdcSGeorge Liu uint16_t bootIndex = 0; 3637647b3cdcSGeorge Liu uint64_t codeIndex = 0; 3638647b3cdcSGeorge Liu if (!parsePostCode(targetID, codeIndex, bootIndex)) 3639a3316fc6SZhikuiRen { 3640a3316fc6SZhikuiRen // Requested ID was not found 3641ace85d60SEd Tanous messages::resourceMissingAtURI(asyncResp->res, req.urlView); 3642a3316fc6SZhikuiRen return; 3643a3316fc6SZhikuiRen } 3644a3316fc6SZhikuiRen if (bootIndex == 0 || codeIndex == 0) 3645a3316fc6SZhikuiRen { 3646a3316fc6SZhikuiRen BMCWEB_LOG_DEBUG << "Get Post Code invalid entry string " 36477e860f15SJohn Edward Broadbent << targetID; 3648a3316fc6SZhikuiRen } 3649a3316fc6SZhikuiRen 3650002d39b4SEd Tanous asyncResp->res.jsonValue["@odata.type"] = "#LogEntry.v1_4_0.LogEntry"; 3651a3316fc6SZhikuiRen asyncResp->res.jsonValue["@odata.id"] = 36520fda0f12SGeorge Liu "/redfish/v1/Systems/system/LogServices/PostCodes/Entries"; 3653a3316fc6SZhikuiRen asyncResp->res.jsonValue["Name"] = "BIOS POST Code Log Entries"; 3654a3316fc6SZhikuiRen asyncResp->res.jsonValue["Description"] = 3655a3316fc6SZhikuiRen "Collection of POST Code Log Entries"; 3656a3316fc6SZhikuiRen asyncResp->res.jsonValue["Members"] = nlohmann::json::array(); 3657a3316fc6SZhikuiRen asyncResp->res.jsonValue["Members@odata.count"] = 0; 3658a3316fc6SZhikuiRen 3659a3316fc6SZhikuiRen getPostCodeForEntry(asyncResp, bootIndex, codeIndex); 36607e860f15SJohn Edward Broadbent }); 3661a3316fc6SZhikuiRen } 3662a3316fc6SZhikuiRen 36631da66f75SEd Tanous } // namespace redfish 3664