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> 309896eaedSEd Tanous #include <boost/algorithm/string/case_conv.hpp> 3111ba3979SEd Tanous #include <boost/algorithm/string/classification.hpp> 32400fd1fbSAdriana Kobylak #include <boost/algorithm/string/replace.hpp> 334851d45dSJason M. Bills #include <boost/algorithm/string/split.hpp> 3407c8c20dSEd Tanous #include <boost/beast/http/verb.hpp> 351da66f75SEd Tanous #include <boost/container/flat_map.hpp> 361ddcf01aSJason M. Bills #include <boost/system/linux_error.hpp> 37168e20c1SEd Tanous #include <dbus_utility.hpp> 38cb92c03bSAndrew Geissler #include <error_messages.hpp> 3945ca1b86SEd Tanous #include <query.hpp> 40ed398213SEd Tanous #include <registries/privilege_registry.hpp> 412b82937eSEd Tanous #include <utils/time_utils.hpp> 421214b7e7SGunnar Mills 43647b3cdcSGeorge Liu #include <charconv> 444418c7f0SJames Feist #include <filesystem> 4575710de2SXiaochao Ma #include <optional> 4626702d01SEd Tanous #include <span> 47cd225da8SJason M. Bills #include <string_view> 48abf2add6SEd Tanous #include <variant> 491da66f75SEd Tanous 501da66f75SEd Tanous namespace redfish 511da66f75SEd Tanous { 521da66f75SEd Tanous 535b61b5e8SJason M. Bills constexpr char const* crashdumpObject = "com.intel.crashdump"; 545b61b5e8SJason M. Bills constexpr char const* crashdumpPath = "/com/intel/crashdump"; 555b61b5e8SJason M. Bills constexpr char const* crashdumpInterface = "com.intel.crashdump"; 565b61b5e8SJason M. Bills constexpr char const* deleteAllInterface = 575b61b5e8SJason M. Bills "xyz.openbmc_project.Collection.DeleteAll"; 585b61b5e8SJason M. Bills constexpr char const* crashdumpOnDemandInterface = 59424c4176SJason M. Bills "com.intel.crashdump.OnDemand"; 606eda7685SKenny L. Ku constexpr char const* crashdumpTelemetryInterface = 616eda7685SKenny L. Ku "com.intel.crashdump.Telemetry"; 621da66f75SEd Tanous 63fffb8c1fSEd Tanous namespace registries 644851d45dSJason M. Bills { 6526702d01SEd Tanous static const Message* 6626702d01SEd Tanous getMessageFromRegistry(const std::string& messageKey, 6726702d01SEd Tanous const std::span<const MessageEntry> registry) 684851d45dSJason M. Bills { 69002d39b4SEd Tanous std::span<const MessageEntry>::iterator messageIt = 70002d39b4SEd Tanous std::find_if(registry.begin(), registry.end(), 714851d45dSJason M. Bills [&messageKey](const MessageEntry& messageEntry) { 72e662eae8SEd Tanous return std::strcmp(messageEntry.first, messageKey.c_str()) == 0; 734851d45dSJason M. Bills }); 7426702d01SEd Tanous if (messageIt != registry.end()) 754851d45dSJason M. Bills { 764851d45dSJason M. Bills return &messageIt->second; 774851d45dSJason M. Bills } 784851d45dSJason M. Bills 794851d45dSJason M. Bills return nullptr; 804851d45dSJason M. Bills } 814851d45dSJason M. Bills 824851d45dSJason M. Bills static const Message* getMessage(const std::string_view& messageID) 834851d45dSJason M. Bills { 844851d45dSJason M. Bills // Redfish MessageIds are in the form 854851d45dSJason M. Bills // RegistryName.MajorVersion.MinorVersion.MessageKey, so parse it to find 864851d45dSJason M. Bills // the right Message 874851d45dSJason M. Bills std::vector<std::string> fields; 884851d45dSJason M. Bills fields.reserve(4); 894851d45dSJason M. Bills boost::split(fields, messageID, boost::is_any_of(".")); 9002cad96eSEd Tanous const std::string& registryName = fields[0]; 9102cad96eSEd Tanous const std::string& messageKey = fields[3]; 924851d45dSJason M. Bills 934851d45dSJason M. Bills // Find the right registry and check it for the MessageKey 944851d45dSJason M. Bills if (std::string(base::header.registryPrefix) == registryName) 954851d45dSJason M. Bills { 964851d45dSJason M. Bills return getMessageFromRegistry( 9726702d01SEd Tanous messageKey, std::span<const MessageEntry>(base::registry)); 984851d45dSJason M. Bills } 994851d45dSJason M. Bills if (std::string(openbmc::header.registryPrefix) == registryName) 1004851d45dSJason M. Bills { 1014851d45dSJason M. Bills return getMessageFromRegistry( 10226702d01SEd Tanous messageKey, std::span<const MessageEntry>(openbmc::registry)); 1034851d45dSJason M. Bills } 1044851d45dSJason M. Bills return nullptr; 1054851d45dSJason M. Bills } 106fffb8c1fSEd Tanous } // namespace registries 1074851d45dSJason M. Bills 108f6150403SJames Feist namespace fs = std::filesystem; 1091da66f75SEd Tanous 110cb92c03bSAndrew Geissler inline std::string translateSeverityDbusToRedfish(const std::string& s) 111cb92c03bSAndrew Geissler { 112d4d25793SEd Tanous if ((s == "xyz.openbmc_project.Logging.Entry.Level.Alert") || 113d4d25793SEd Tanous (s == "xyz.openbmc_project.Logging.Entry.Level.Critical") || 114d4d25793SEd Tanous (s == "xyz.openbmc_project.Logging.Entry.Level.Emergency") || 115d4d25793SEd Tanous (s == "xyz.openbmc_project.Logging.Entry.Level.Error")) 116cb92c03bSAndrew Geissler { 117cb92c03bSAndrew Geissler return "Critical"; 118cb92c03bSAndrew Geissler } 1193174e4dfSEd Tanous if ((s == "xyz.openbmc_project.Logging.Entry.Level.Debug") || 120d4d25793SEd Tanous (s == "xyz.openbmc_project.Logging.Entry.Level.Informational") || 121d4d25793SEd Tanous (s == "xyz.openbmc_project.Logging.Entry.Level.Notice")) 122cb92c03bSAndrew Geissler { 123cb92c03bSAndrew Geissler return "OK"; 124cb92c03bSAndrew Geissler } 1253174e4dfSEd Tanous if (s == "xyz.openbmc_project.Logging.Entry.Level.Warning") 126cb92c03bSAndrew Geissler { 127cb92c03bSAndrew Geissler return "Warning"; 128cb92c03bSAndrew Geissler } 129cb92c03bSAndrew Geissler return ""; 130cb92c03bSAndrew Geissler } 131cb92c03bSAndrew Geissler 1327e860f15SJohn Edward Broadbent inline static int getJournalMetadata(sd_journal* journal, 13339e77504SEd Tanous const std::string_view& field, 13439e77504SEd Tanous std::string_view& contents) 13516428a1aSJason M. Bills { 13616428a1aSJason M. Bills const char* data = nullptr; 13716428a1aSJason M. Bills size_t length = 0; 13816428a1aSJason M. Bills int ret = 0; 13916428a1aSJason M. Bills // Get the metadata from the requested field of the journal entry 14046ff87baSEd Tanous // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast) 14146ff87baSEd Tanous const void** dataVoid = reinterpret_cast<const void**>(&data); 14246ff87baSEd Tanous 14346ff87baSEd Tanous ret = sd_journal_get_data(journal, field.data(), dataVoid, &length); 14416428a1aSJason M. Bills if (ret < 0) 14516428a1aSJason M. Bills { 14616428a1aSJason M. Bills return ret; 14716428a1aSJason M. Bills } 14839e77504SEd Tanous contents = std::string_view(data, length); 14916428a1aSJason M. Bills // Only use the content after the "=" character. 15081ce609eSEd Tanous contents.remove_prefix(std::min(contents.find('=') + 1, contents.size())); 15116428a1aSJason M. Bills return ret; 15216428a1aSJason M. Bills } 15316428a1aSJason M. Bills 1547e860f15SJohn Edward Broadbent inline static int getJournalMetadata(sd_journal* journal, 1557e860f15SJohn Edward Broadbent const std::string_view& field, 1567e860f15SJohn Edward Broadbent const int& base, long int& contents) 15716428a1aSJason M. Bills { 15816428a1aSJason M. Bills int ret = 0; 15939e77504SEd Tanous std::string_view metadata; 16016428a1aSJason M. Bills // Get the metadata from the requested field of the journal entry 16116428a1aSJason M. Bills ret = getJournalMetadata(journal, field, metadata); 16216428a1aSJason M. Bills if (ret < 0) 16316428a1aSJason M. Bills { 16416428a1aSJason M. Bills return ret; 16516428a1aSJason M. Bills } 166b01bf299SEd Tanous contents = strtol(metadata.data(), nullptr, base); 16716428a1aSJason M. Bills return ret; 16816428a1aSJason M. Bills } 16916428a1aSJason M. Bills 1707e860f15SJohn Edward Broadbent inline static bool getEntryTimestamp(sd_journal* journal, 1717e860f15SJohn Edward Broadbent std::string& entryTimestamp) 172a3316fc6SZhikuiRen { 173a3316fc6SZhikuiRen int ret = 0; 174a3316fc6SZhikuiRen uint64_t timestamp = 0; 175a3316fc6SZhikuiRen ret = sd_journal_get_realtime_usec(journal, ×tamp); 176a3316fc6SZhikuiRen if (ret < 0) 177a3316fc6SZhikuiRen { 178a3316fc6SZhikuiRen BMCWEB_LOG_ERROR << "Failed to read entry timestamp: " 179a3316fc6SZhikuiRen << strerror(-ret); 180a3316fc6SZhikuiRen return false; 181a3316fc6SZhikuiRen } 1822b82937eSEd Tanous entryTimestamp = 1832b82937eSEd Tanous redfish::time_utils::getDateTimeUint(timestamp / 1000 / 1000); 1849c620e21SAsmitha Karunanithi return true; 185a3316fc6SZhikuiRen } 18650b8a43aSEd Tanous 1877e860f15SJohn Edward Broadbent inline static bool getUniqueEntryID(sd_journal* journal, std::string& entryID, 188e85d6b16SJason M. Bills const bool firstEntry = true) 18916428a1aSJason M. Bills { 19016428a1aSJason M. Bills int ret = 0; 19116428a1aSJason M. Bills static uint64_t prevTs = 0; 19216428a1aSJason M. Bills static int index = 0; 193e85d6b16SJason M. Bills if (firstEntry) 194e85d6b16SJason M. Bills { 195e85d6b16SJason M. Bills prevTs = 0; 196e85d6b16SJason M. Bills } 197e85d6b16SJason M. Bills 19816428a1aSJason M. Bills // Get the entry timestamp 19916428a1aSJason M. Bills uint64_t curTs = 0; 20016428a1aSJason M. Bills ret = sd_journal_get_realtime_usec(journal, &curTs); 20116428a1aSJason M. Bills if (ret < 0) 20216428a1aSJason M. Bills { 20316428a1aSJason M. Bills BMCWEB_LOG_ERROR << "Failed to read entry timestamp: " 20416428a1aSJason M. Bills << strerror(-ret); 20516428a1aSJason M. Bills return false; 20616428a1aSJason M. Bills } 20716428a1aSJason M. Bills // If the timestamp isn't unique, increment the index 20816428a1aSJason M. Bills if (curTs == prevTs) 20916428a1aSJason M. Bills { 21016428a1aSJason M. Bills index++; 21116428a1aSJason M. Bills } 21216428a1aSJason M. Bills else 21316428a1aSJason M. Bills { 21416428a1aSJason M. Bills // Otherwise, reset it 21516428a1aSJason M. Bills index = 0; 21616428a1aSJason M. Bills } 21716428a1aSJason M. Bills // Save the timestamp 21816428a1aSJason M. Bills prevTs = curTs; 21916428a1aSJason M. Bills 22016428a1aSJason M. Bills entryID = std::to_string(curTs); 22116428a1aSJason M. Bills if (index > 0) 22216428a1aSJason M. Bills { 22316428a1aSJason M. Bills entryID += "_" + std::to_string(index); 22416428a1aSJason M. Bills } 22516428a1aSJason M. Bills return true; 22616428a1aSJason M. Bills } 22716428a1aSJason M. Bills 228e85d6b16SJason M. Bills static bool getUniqueEntryID(const std::string& logEntry, std::string& entryID, 229e85d6b16SJason M. Bills const bool firstEntry = true) 23095820184SJason M. Bills { 231271584abSEd Tanous static time_t prevTs = 0; 23295820184SJason M. Bills static int index = 0; 233e85d6b16SJason M. Bills if (firstEntry) 234e85d6b16SJason M. Bills { 235e85d6b16SJason M. Bills prevTs = 0; 236e85d6b16SJason M. Bills } 237e85d6b16SJason M. Bills 23895820184SJason M. Bills // Get the entry timestamp 239271584abSEd Tanous std::time_t curTs = 0; 24095820184SJason M. Bills std::tm timeStruct = {}; 24195820184SJason M. Bills std::istringstream entryStream(logEntry); 24295820184SJason M. Bills if (entryStream >> std::get_time(&timeStruct, "%Y-%m-%dT%H:%M:%S")) 24395820184SJason M. Bills { 24495820184SJason M. Bills curTs = std::mktime(&timeStruct); 24595820184SJason M. Bills } 24695820184SJason M. Bills // If the timestamp isn't unique, increment the index 24795820184SJason M. Bills if (curTs == prevTs) 24895820184SJason M. Bills { 24995820184SJason M. Bills index++; 25095820184SJason M. Bills } 25195820184SJason M. Bills else 25295820184SJason M. Bills { 25395820184SJason M. Bills // Otherwise, reset it 25495820184SJason M. Bills index = 0; 25595820184SJason M. Bills } 25695820184SJason M. Bills // Save the timestamp 25795820184SJason M. Bills prevTs = curTs; 25895820184SJason M. Bills 25995820184SJason M. Bills entryID = std::to_string(curTs); 26095820184SJason M. Bills if (index > 0) 26195820184SJason M. Bills { 26295820184SJason M. Bills entryID += "_" + std::to_string(index); 26395820184SJason M. Bills } 26495820184SJason M. Bills return true; 26595820184SJason M. Bills } 26695820184SJason M. Bills 2677e860f15SJohn Edward Broadbent inline static bool 2688d1b46d7Szhanghch05 getTimestampFromID(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp, 2698d1b46d7Szhanghch05 const std::string& entryID, uint64_t& timestamp, 2708d1b46d7Szhanghch05 uint64_t& index) 27116428a1aSJason M. Bills { 27216428a1aSJason M. Bills if (entryID.empty()) 27316428a1aSJason M. Bills { 27416428a1aSJason M. Bills return false; 27516428a1aSJason M. Bills } 27616428a1aSJason M. Bills // Convert the unique ID back to a timestamp to find the entry 27739e77504SEd Tanous std::string_view tsStr(entryID); 27816428a1aSJason M. Bills 27981ce609eSEd Tanous auto underscorePos = tsStr.find('_'); 28071d5d8dbSEd Tanous if (underscorePos != std::string_view::npos) 28116428a1aSJason M. Bills { 28216428a1aSJason M. Bills // Timestamp has an index 28316428a1aSJason M. Bills tsStr.remove_suffix(tsStr.size() - underscorePos); 28439e77504SEd Tanous std::string_view indexStr(entryID); 28516428a1aSJason M. Bills indexStr.remove_prefix(underscorePos + 1); 286c0bd5e4bSEd Tanous auto [ptr, ec] = std::from_chars( 287c0bd5e4bSEd Tanous indexStr.data(), indexStr.data() + indexStr.size(), index); 288c0bd5e4bSEd Tanous if (ec != std::errc()) 28916428a1aSJason M. Bills { 290ace85d60SEd Tanous messages::resourceMissingAtURI( 291ace85d60SEd Tanous asyncResp->res, crow::utility::urlFromPieces(entryID)); 29216428a1aSJason M. Bills return false; 29316428a1aSJason M. Bills } 29416428a1aSJason M. Bills } 29516428a1aSJason M. Bills // Timestamp has no index 296c0bd5e4bSEd Tanous auto [ptr, ec] = 297c0bd5e4bSEd Tanous std::from_chars(tsStr.data(), tsStr.data() + tsStr.size(), timestamp); 298c0bd5e4bSEd Tanous if (ec != std::errc()) 29916428a1aSJason M. Bills { 300ace85d60SEd Tanous messages::resourceMissingAtURI(asyncResp->res, 301ace85d60SEd Tanous crow::utility::urlFromPieces(entryID)); 30216428a1aSJason M. Bills return false; 30316428a1aSJason M. Bills } 30416428a1aSJason M. Bills return true; 30516428a1aSJason M. Bills } 30616428a1aSJason M. Bills 30795820184SJason M. Bills static bool 30895820184SJason M. Bills getRedfishLogFiles(std::vector<std::filesystem::path>& redfishLogFiles) 30995820184SJason M. Bills { 31095820184SJason M. Bills static const std::filesystem::path redfishLogDir = "/var/log"; 31195820184SJason M. Bills static const std::string redfishLogFilename = "redfish"; 31295820184SJason M. Bills 31395820184SJason M. Bills // Loop through the directory looking for redfish log files 31495820184SJason M. Bills for (const std::filesystem::directory_entry& dirEnt : 31595820184SJason M. Bills std::filesystem::directory_iterator(redfishLogDir)) 31695820184SJason M. Bills { 31795820184SJason M. Bills // If we find a redfish log file, save the path 31895820184SJason M. Bills std::string filename = dirEnt.path().filename(); 31911ba3979SEd Tanous if (filename.starts_with(redfishLogFilename)) 32095820184SJason M. Bills { 32195820184SJason M. Bills redfishLogFiles.emplace_back(redfishLogDir / filename); 32295820184SJason M. Bills } 32395820184SJason M. Bills } 32495820184SJason M. Bills // As the log files rotate, they are appended with a ".#" that is higher for 32595820184SJason M. Bills // the older logs. Since we don't expect more than 10 log files, we 32695820184SJason M. Bills // can just sort the list to get them in order from newest to oldest 32795820184SJason M. Bills std::sort(redfishLogFiles.begin(), redfishLogFiles.end()); 32895820184SJason M. Bills 32995820184SJason M. Bills return !redfishLogFiles.empty(); 33095820184SJason M. Bills } 33195820184SJason M. Bills 332aefe3786SClaire Weinan inline void parseDumpEntryFromDbusObject( 3332d613eb6SJiaqing Zhao const dbus::utility::ManagedObjectType::value_type& object, 334*c6fecdabSClaire Weinan std::string& dumpStatus, uint64_t& size, uint64_t& timestampUs, 335aefe3786SClaire Weinan const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) 336aefe3786SClaire Weinan { 337aefe3786SClaire Weinan for (const auto& interfaceMap : object.second) 338aefe3786SClaire Weinan { 339aefe3786SClaire Weinan if (interfaceMap.first == "xyz.openbmc_project.Common.Progress") 340aefe3786SClaire Weinan { 341aefe3786SClaire Weinan for (const auto& propertyMap : interfaceMap.second) 342aefe3786SClaire Weinan { 343aefe3786SClaire Weinan if (propertyMap.first == "Status") 344aefe3786SClaire Weinan { 345aefe3786SClaire Weinan const auto* status = 346aefe3786SClaire Weinan std::get_if<std::string>(&propertyMap.second); 347aefe3786SClaire Weinan if (status == nullptr) 348aefe3786SClaire Weinan { 349aefe3786SClaire Weinan messages::internalError(asyncResp->res); 350aefe3786SClaire Weinan break; 351aefe3786SClaire Weinan } 352aefe3786SClaire Weinan dumpStatus = *status; 353aefe3786SClaire Weinan } 354aefe3786SClaire Weinan } 355aefe3786SClaire Weinan } 356aefe3786SClaire Weinan else if (interfaceMap.first == "xyz.openbmc_project.Dump.Entry") 357aefe3786SClaire Weinan { 358aefe3786SClaire Weinan for (const auto& propertyMap : interfaceMap.second) 359aefe3786SClaire Weinan { 360aefe3786SClaire Weinan if (propertyMap.first == "Size") 361aefe3786SClaire Weinan { 362aefe3786SClaire Weinan const auto* sizePtr = 363aefe3786SClaire Weinan std::get_if<uint64_t>(&propertyMap.second); 364aefe3786SClaire Weinan if (sizePtr == nullptr) 365aefe3786SClaire Weinan { 366aefe3786SClaire Weinan messages::internalError(asyncResp->res); 367aefe3786SClaire Weinan break; 368aefe3786SClaire Weinan } 369aefe3786SClaire Weinan size = *sizePtr; 370aefe3786SClaire Weinan break; 371aefe3786SClaire Weinan } 372aefe3786SClaire Weinan } 373aefe3786SClaire Weinan } 374aefe3786SClaire Weinan else if (interfaceMap.first == "xyz.openbmc_project.Time.EpochTime") 375aefe3786SClaire Weinan { 376aefe3786SClaire Weinan for (const auto& propertyMap : interfaceMap.second) 377aefe3786SClaire Weinan { 378aefe3786SClaire Weinan if (propertyMap.first == "Elapsed") 379aefe3786SClaire Weinan { 380aefe3786SClaire Weinan const uint64_t* usecsTimeStamp = 381aefe3786SClaire Weinan std::get_if<uint64_t>(&propertyMap.second); 382aefe3786SClaire Weinan if (usecsTimeStamp == nullptr) 383aefe3786SClaire Weinan { 384aefe3786SClaire Weinan messages::internalError(asyncResp->res); 385aefe3786SClaire Weinan break; 386aefe3786SClaire Weinan } 387*c6fecdabSClaire Weinan timestampUs = *usecsTimeStamp; 388aefe3786SClaire Weinan break; 389aefe3786SClaire Weinan } 390aefe3786SClaire Weinan } 391aefe3786SClaire Weinan } 392aefe3786SClaire Weinan } 393aefe3786SClaire Weinan } 394aefe3786SClaire Weinan 39521ab404cSNan Zhou static std::string getDumpEntriesPath(const std::string& dumpType) 396fdd26906SClaire Weinan { 397fdd26906SClaire Weinan std::string entriesPath; 398fdd26906SClaire Weinan 399fdd26906SClaire Weinan if (dumpType == "BMC") 400fdd26906SClaire Weinan { 401fdd26906SClaire Weinan entriesPath = "/redfish/v1/Managers/bmc/LogServices/Dump/Entries/"; 402fdd26906SClaire Weinan } 403fdd26906SClaire Weinan else if (dumpType == "FaultLog") 404fdd26906SClaire Weinan { 405fdd26906SClaire Weinan entriesPath = "/redfish/v1/Managers/bmc/LogServices/FaultLog/Entries/"; 406fdd26906SClaire Weinan } 407fdd26906SClaire Weinan else if (dumpType == "System") 408fdd26906SClaire Weinan { 409fdd26906SClaire Weinan entriesPath = "/redfish/v1/Systems/system/LogServices/Dump/Entries/"; 410fdd26906SClaire Weinan } 411fdd26906SClaire Weinan else 412fdd26906SClaire Weinan { 413fdd26906SClaire Weinan BMCWEB_LOG_ERROR << "getDumpEntriesPath() invalid dump type: " 414fdd26906SClaire Weinan << dumpType; 415fdd26906SClaire Weinan } 416fdd26906SClaire Weinan 417fdd26906SClaire Weinan // Returns empty string on error 418fdd26906SClaire Weinan return entriesPath; 419fdd26906SClaire Weinan } 420fdd26906SClaire Weinan 4218d1b46d7Szhanghch05 inline void 4228d1b46d7Szhanghch05 getDumpEntryCollection(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp, 4235cb1dd27SAsmitha Karunanithi const std::string& dumpType) 4245cb1dd27SAsmitha Karunanithi { 425fdd26906SClaire Weinan std::string entriesPath = getDumpEntriesPath(dumpType); 426fdd26906SClaire Weinan if (entriesPath.empty()) 4275cb1dd27SAsmitha Karunanithi { 4285cb1dd27SAsmitha Karunanithi messages::internalError(asyncResp->res); 4295cb1dd27SAsmitha Karunanithi return; 4305cb1dd27SAsmitha Karunanithi } 4315cb1dd27SAsmitha Karunanithi 4325cb1dd27SAsmitha Karunanithi crow::connections::systemBus->async_method_call( 433fdd26906SClaire Weinan [asyncResp, entriesPath, 434711ac7a9SEd Tanous dumpType](const boost::system::error_code ec, 435711ac7a9SEd Tanous dbus::utility::ManagedObjectType& resp) { 4365cb1dd27SAsmitha Karunanithi if (ec) 4375cb1dd27SAsmitha Karunanithi { 4385cb1dd27SAsmitha Karunanithi BMCWEB_LOG_ERROR << "DumpEntry resp_handler got error " << ec; 4395cb1dd27SAsmitha Karunanithi messages::internalError(asyncResp->res); 4405cb1dd27SAsmitha Karunanithi return; 4415cb1dd27SAsmitha Karunanithi } 4425cb1dd27SAsmitha Karunanithi 443fdd26906SClaire Weinan // Remove ending slash 444fdd26906SClaire Weinan std::string odataIdStr = entriesPath; 445fdd26906SClaire Weinan if (!odataIdStr.empty()) 446fdd26906SClaire Weinan { 447fdd26906SClaire Weinan odataIdStr.pop_back(); 448fdd26906SClaire Weinan } 449fdd26906SClaire Weinan 450fdd26906SClaire Weinan asyncResp->res.jsonValue["@odata.type"] = 451fdd26906SClaire Weinan "#LogEntryCollection.LogEntryCollection"; 452fdd26906SClaire Weinan asyncResp->res.jsonValue["@odata.id"] = std::move(odataIdStr); 453fdd26906SClaire Weinan asyncResp->res.jsonValue["Name"] = dumpType + " Dump Entries"; 454fdd26906SClaire Weinan asyncResp->res.jsonValue["Description"] = 455fdd26906SClaire Weinan "Collection of " + dumpType + " Dump Entries"; 456fdd26906SClaire Weinan 4575cb1dd27SAsmitha Karunanithi nlohmann::json& entriesArray = asyncResp->res.jsonValue["Members"]; 4585cb1dd27SAsmitha Karunanithi entriesArray = nlohmann::json::array(); 459b47452b2SAsmitha Karunanithi std::string dumpEntryPath = 460b47452b2SAsmitha Karunanithi "/xyz/openbmc_project/dump/" + 461002d39b4SEd Tanous std::string(boost::algorithm::to_lower_copy(dumpType)) + "/entry/"; 4625cb1dd27SAsmitha Karunanithi 463002d39b4SEd Tanous std::sort(resp.begin(), resp.end(), [](const auto& l, const auto& r) { 464002d39b4SEd Tanous return AlphanumLess<std::string>()(l.first.filename(), 465002d39b4SEd Tanous r.first.filename()); 466565dfb6fSClaire Weinan }); 467565dfb6fSClaire Weinan 4685cb1dd27SAsmitha Karunanithi for (auto& object : resp) 4695cb1dd27SAsmitha Karunanithi { 470b47452b2SAsmitha Karunanithi if (object.first.str.find(dumpEntryPath) == std::string::npos) 4715cb1dd27SAsmitha Karunanithi { 4725cb1dd27SAsmitha Karunanithi continue; 4735cb1dd27SAsmitha Karunanithi } 474*c6fecdabSClaire Weinan uint64_t timestampUs = 0; 4755cb1dd27SAsmitha Karunanithi uint64_t size = 0; 47635440d18SAsmitha Karunanithi std::string dumpStatus; 477433b68b4SJason M. Bills nlohmann::json::object_t thisEntry; 4782dfd18efSEd Tanous 4792dfd18efSEd Tanous std::string entryID = object.first.filename(); 4802dfd18efSEd Tanous if (entryID.empty()) 4815cb1dd27SAsmitha Karunanithi { 4825cb1dd27SAsmitha Karunanithi continue; 4835cb1dd27SAsmitha Karunanithi } 4845cb1dd27SAsmitha Karunanithi 485*c6fecdabSClaire Weinan parseDumpEntryFromDbusObject(object, dumpStatus, size, timestampUs, 486aefe3786SClaire Weinan asyncResp); 4875cb1dd27SAsmitha Karunanithi 4880fda0f12SGeorge Liu if (dumpStatus != 4890fda0f12SGeorge Liu "xyz.openbmc_project.Common.Progress.OperationStatus.Completed" && 49035440d18SAsmitha Karunanithi !dumpStatus.empty()) 49135440d18SAsmitha Karunanithi { 49235440d18SAsmitha Karunanithi // Dump status is not Complete, no need to enumerate 49335440d18SAsmitha Karunanithi continue; 49435440d18SAsmitha Karunanithi } 49535440d18SAsmitha Karunanithi 496647b3cdcSGeorge Liu thisEntry["@odata.type"] = "#LogEntry.v1_8_0.LogEntry"; 497fdd26906SClaire Weinan thisEntry["@odata.id"] = entriesPath + entryID; 4985cb1dd27SAsmitha Karunanithi thisEntry["Id"] = entryID; 4995cb1dd27SAsmitha Karunanithi thisEntry["EntryType"] = "Event"; 5005cb1dd27SAsmitha Karunanithi thisEntry["Name"] = dumpType + " Dump Entry"; 5015cb1dd27SAsmitha Karunanithi 5025cb1dd27SAsmitha Karunanithi if (dumpType == "BMC") 5035cb1dd27SAsmitha Karunanithi { 504*c6fecdabSClaire Weinan thisEntry["Created"] = redfish::time_utils::getDateTimeUint( 505*c6fecdabSClaire Weinan timestampUs / 1000 / 1000); 506d337bb72SAsmitha Karunanithi thisEntry["DiagnosticDataType"] = "Manager"; 507d337bb72SAsmitha Karunanithi thisEntry["AdditionalDataURI"] = 508fdd26906SClaire Weinan entriesPath + entryID + "/attachment"; 509fdd26906SClaire Weinan thisEntry["AdditionalDataSizeBytes"] = size; 5105cb1dd27SAsmitha Karunanithi } 511*c6fecdabSClaire Weinan else if (dumpType == "FaultLog") 512*c6fecdabSClaire Weinan { 513*c6fecdabSClaire Weinan thisEntry["Created"] = 514*c6fecdabSClaire Weinan redfish::time_utils::getDateTimeUintUs(timestampUs); 515*c6fecdabSClaire Weinan } 5165cb1dd27SAsmitha Karunanithi else if (dumpType == "System") 5175cb1dd27SAsmitha Karunanithi { 518*c6fecdabSClaire Weinan thisEntry["Created"] = redfish::time_utils::getDateTimeUint( 519*c6fecdabSClaire Weinan timestampUs / 1000 / 1000); 520d337bb72SAsmitha Karunanithi thisEntry["DiagnosticDataType"] = "OEM"; 521d337bb72SAsmitha Karunanithi thisEntry["OEMDiagnosticDataType"] = "System"; 522d337bb72SAsmitha Karunanithi thisEntry["AdditionalDataURI"] = 523fdd26906SClaire Weinan entriesPath + entryID + "/attachment"; 524fdd26906SClaire Weinan thisEntry["AdditionalDataSizeBytes"] = size; 5255cb1dd27SAsmitha Karunanithi } 52635440d18SAsmitha Karunanithi entriesArray.push_back(std::move(thisEntry)); 5275cb1dd27SAsmitha Karunanithi } 528002d39b4SEd Tanous asyncResp->res.jsonValue["Members@odata.count"] = entriesArray.size(); 5295cb1dd27SAsmitha Karunanithi }, 5305cb1dd27SAsmitha Karunanithi "xyz.openbmc_project.Dump.Manager", "/xyz/openbmc_project/dump", 5315cb1dd27SAsmitha Karunanithi "org.freedesktop.DBus.ObjectManager", "GetManagedObjects"); 5325cb1dd27SAsmitha Karunanithi } 5335cb1dd27SAsmitha Karunanithi 5348d1b46d7Szhanghch05 inline void 535c7a6d660SClaire Weinan getDumpEntryById(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp, 5368d1b46d7Szhanghch05 const std::string& entryID, const std::string& dumpType) 5375cb1dd27SAsmitha Karunanithi { 538fdd26906SClaire Weinan std::string entriesPath = getDumpEntriesPath(dumpType); 539fdd26906SClaire Weinan if (entriesPath.empty()) 5405cb1dd27SAsmitha Karunanithi { 5415cb1dd27SAsmitha Karunanithi messages::internalError(asyncResp->res); 5425cb1dd27SAsmitha Karunanithi return; 5435cb1dd27SAsmitha Karunanithi } 5445cb1dd27SAsmitha Karunanithi 5455cb1dd27SAsmitha Karunanithi crow::connections::systemBus->async_method_call( 546fdd26906SClaire Weinan [asyncResp, entryID, dumpType, 547fdd26906SClaire Weinan entriesPath](const boost::system::error_code ec, 54802cad96eSEd Tanous const dbus::utility::ManagedObjectType& resp) { 5495cb1dd27SAsmitha Karunanithi if (ec) 5505cb1dd27SAsmitha Karunanithi { 5515cb1dd27SAsmitha Karunanithi BMCWEB_LOG_ERROR << "DumpEntry resp_handler got error " << ec; 5525cb1dd27SAsmitha Karunanithi messages::internalError(asyncResp->res); 5535cb1dd27SAsmitha Karunanithi return; 5545cb1dd27SAsmitha Karunanithi } 5555cb1dd27SAsmitha Karunanithi 556b47452b2SAsmitha Karunanithi bool foundDumpEntry = false; 557b47452b2SAsmitha Karunanithi std::string dumpEntryPath = 558b47452b2SAsmitha Karunanithi "/xyz/openbmc_project/dump/" + 559002d39b4SEd Tanous std::string(boost::algorithm::to_lower_copy(dumpType)) + "/entry/"; 560b47452b2SAsmitha Karunanithi 5619eb808c1SEd Tanous for (const auto& objectPath : resp) 5625cb1dd27SAsmitha Karunanithi { 563b47452b2SAsmitha Karunanithi if (objectPath.first.str != dumpEntryPath + entryID) 5645cb1dd27SAsmitha Karunanithi { 5655cb1dd27SAsmitha Karunanithi continue; 5665cb1dd27SAsmitha Karunanithi } 5675cb1dd27SAsmitha Karunanithi 5685cb1dd27SAsmitha Karunanithi foundDumpEntry = true; 569*c6fecdabSClaire Weinan uint64_t timestampUs = 0; 5705cb1dd27SAsmitha Karunanithi uint64_t size = 0; 57135440d18SAsmitha Karunanithi std::string dumpStatus; 5725cb1dd27SAsmitha Karunanithi 573aefe3786SClaire Weinan parseDumpEntryFromDbusObject(objectPath, dumpStatus, size, 574*c6fecdabSClaire Weinan timestampUs, asyncResp); 5755cb1dd27SAsmitha Karunanithi 5760fda0f12SGeorge Liu if (dumpStatus != 5770fda0f12SGeorge Liu "xyz.openbmc_project.Common.Progress.OperationStatus.Completed" && 57835440d18SAsmitha Karunanithi !dumpStatus.empty()) 57935440d18SAsmitha Karunanithi { 58035440d18SAsmitha Karunanithi // Dump status is not Complete 58135440d18SAsmitha Karunanithi // return not found until status is changed to Completed 582d8a5d5d8SJiaqing Zhao messages::resourceNotFound(asyncResp->res, "LogEntry", entryID); 58335440d18SAsmitha Karunanithi return; 58435440d18SAsmitha Karunanithi } 58535440d18SAsmitha Karunanithi 5865cb1dd27SAsmitha Karunanithi asyncResp->res.jsonValue["@odata.type"] = 587647b3cdcSGeorge Liu "#LogEntry.v1_8_0.LogEntry"; 588fdd26906SClaire Weinan asyncResp->res.jsonValue["@odata.id"] = entriesPath + entryID; 5895cb1dd27SAsmitha Karunanithi asyncResp->res.jsonValue["Id"] = entryID; 5905cb1dd27SAsmitha Karunanithi asyncResp->res.jsonValue["EntryType"] = "Event"; 5915cb1dd27SAsmitha Karunanithi asyncResp->res.jsonValue["Name"] = dumpType + " Dump Entry"; 5925cb1dd27SAsmitha Karunanithi 5935cb1dd27SAsmitha Karunanithi if (dumpType == "BMC") 5945cb1dd27SAsmitha Karunanithi { 595*c6fecdabSClaire Weinan asyncResp->res.jsonValue["Created"] = 596*c6fecdabSClaire Weinan redfish::time_utils::getDateTimeUint(timestampUs / 1000 / 597*c6fecdabSClaire Weinan 1000); 598d337bb72SAsmitha Karunanithi asyncResp->res.jsonValue["DiagnosticDataType"] = "Manager"; 599d337bb72SAsmitha Karunanithi asyncResp->res.jsonValue["AdditionalDataURI"] = 600fdd26906SClaire Weinan entriesPath + entryID + "/attachment"; 601fdd26906SClaire Weinan asyncResp->res.jsonValue["AdditionalDataSizeBytes"] = size; 6025cb1dd27SAsmitha Karunanithi } 603*c6fecdabSClaire Weinan else if (dumpType == "FaultLog") 604*c6fecdabSClaire Weinan { 605*c6fecdabSClaire Weinan asyncResp->res.jsonValue["Created"] = 606*c6fecdabSClaire Weinan redfish::time_utils::getDateTimeUintUs(timestampUs); 607*c6fecdabSClaire Weinan } 6085cb1dd27SAsmitha Karunanithi else if (dumpType == "System") 6095cb1dd27SAsmitha Karunanithi { 610*c6fecdabSClaire Weinan asyncResp->res.jsonValue["Created"] = 611*c6fecdabSClaire Weinan redfish::time_utils::getDateTimeUint(timestampUs / 1000 / 612*c6fecdabSClaire Weinan 1000); 613d337bb72SAsmitha Karunanithi asyncResp->res.jsonValue["DiagnosticDataType"] = "OEM"; 614002d39b4SEd Tanous asyncResp->res.jsonValue["OEMDiagnosticDataType"] = "System"; 615d337bb72SAsmitha Karunanithi asyncResp->res.jsonValue["AdditionalDataURI"] = 616fdd26906SClaire Weinan entriesPath + entryID + "/attachment"; 617fdd26906SClaire Weinan asyncResp->res.jsonValue["AdditionalDataSizeBytes"] = size; 6185cb1dd27SAsmitha Karunanithi } 6195cb1dd27SAsmitha Karunanithi } 620e05aec50SEd Tanous if (!foundDumpEntry) 621b47452b2SAsmitha Karunanithi { 622b47452b2SAsmitha Karunanithi BMCWEB_LOG_ERROR << "Can't find Dump Entry"; 623b47452b2SAsmitha Karunanithi messages::internalError(asyncResp->res); 624b47452b2SAsmitha Karunanithi return; 625b47452b2SAsmitha Karunanithi } 6265cb1dd27SAsmitha Karunanithi }, 6275cb1dd27SAsmitha Karunanithi "xyz.openbmc_project.Dump.Manager", "/xyz/openbmc_project/dump", 6285cb1dd27SAsmitha Karunanithi "org.freedesktop.DBus.ObjectManager", "GetManagedObjects"); 6295cb1dd27SAsmitha Karunanithi } 6305cb1dd27SAsmitha Karunanithi 6318d1b46d7Szhanghch05 inline void deleteDumpEntry(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp, 6329878256fSStanley Chu const std::string& entryID, 633b47452b2SAsmitha Karunanithi const std::string& dumpType) 6345cb1dd27SAsmitha Karunanithi { 635002d39b4SEd Tanous auto respHandler = 636002d39b4SEd Tanous [asyncResp, entryID](const boost::system::error_code ec) { 6375cb1dd27SAsmitha Karunanithi BMCWEB_LOG_DEBUG << "Dump Entry doDelete callback: Done"; 6385cb1dd27SAsmitha Karunanithi if (ec) 6395cb1dd27SAsmitha Karunanithi { 6403de8d8baSGeorge Liu if (ec.value() == EBADR) 6413de8d8baSGeorge Liu { 6423de8d8baSGeorge Liu messages::resourceNotFound(asyncResp->res, "LogEntry", entryID); 6433de8d8baSGeorge Liu return; 6443de8d8baSGeorge Liu } 6455cb1dd27SAsmitha Karunanithi BMCWEB_LOG_ERROR << "Dump (DBus) doDelete respHandler got error " 646fdd26906SClaire Weinan << ec << " entryID=" << entryID; 6475cb1dd27SAsmitha Karunanithi messages::internalError(asyncResp->res); 6485cb1dd27SAsmitha Karunanithi return; 6495cb1dd27SAsmitha Karunanithi } 6505cb1dd27SAsmitha Karunanithi }; 6515cb1dd27SAsmitha Karunanithi crow::connections::systemBus->async_method_call( 6525cb1dd27SAsmitha Karunanithi respHandler, "xyz.openbmc_project.Dump.Manager", 653b47452b2SAsmitha Karunanithi "/xyz/openbmc_project/dump/" + 654b47452b2SAsmitha Karunanithi std::string(boost::algorithm::to_lower_copy(dumpType)) + "/entry/" + 655b47452b2SAsmitha Karunanithi entryID, 6565cb1dd27SAsmitha Karunanithi "xyz.openbmc_project.Object.Delete", "Delete"); 6575cb1dd27SAsmitha Karunanithi } 6585cb1dd27SAsmitha Karunanithi 6598d1b46d7Szhanghch05 inline void 66098be3e39SEd Tanous createDumpTaskCallback(task::Payload&& payload, 6618d1b46d7Szhanghch05 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp, 6628d1b46d7Szhanghch05 const uint32_t& dumpId, const std::string& dumpPath, 663a43be80fSAsmitha Karunanithi const std::string& dumpType) 664a43be80fSAsmitha Karunanithi { 665a43be80fSAsmitha Karunanithi std::shared_ptr<task::TaskData> task = task::TaskData::createTask( 66659d494eeSPatrick Williams [dumpId, dumpPath, 66759d494eeSPatrick Williams dumpType](boost::system::error_code err, sdbusplus::message_t& m, 668a43be80fSAsmitha Karunanithi const std::shared_ptr<task::TaskData>& taskData) { 669cb13a392SEd Tanous if (err) 670cb13a392SEd Tanous { 6716145ed6fSAsmitha Karunanithi BMCWEB_LOG_ERROR << "Error in creating a dump"; 6726145ed6fSAsmitha Karunanithi taskData->state = "Cancelled"; 6736145ed6fSAsmitha Karunanithi return task::completed; 674cb13a392SEd Tanous } 675b9d36b47SEd Tanous 676b9d36b47SEd Tanous dbus::utility::DBusInteracesMap interfacesList; 677a43be80fSAsmitha Karunanithi 678a43be80fSAsmitha Karunanithi sdbusplus::message::object_path objPath; 679a43be80fSAsmitha Karunanithi 680a43be80fSAsmitha Karunanithi m.read(objPath, interfacesList); 681a43be80fSAsmitha Karunanithi 682b47452b2SAsmitha Karunanithi if (objPath.str == 683b47452b2SAsmitha Karunanithi "/xyz/openbmc_project/dump/" + 684b47452b2SAsmitha Karunanithi std::string(boost::algorithm::to_lower_copy(dumpType)) + 685b47452b2SAsmitha Karunanithi "/entry/" + std::to_string(dumpId)) 686a43be80fSAsmitha Karunanithi { 687a43be80fSAsmitha Karunanithi nlohmann::json retMessage = messages::success(); 688a43be80fSAsmitha Karunanithi taskData->messages.emplace_back(retMessage); 689a43be80fSAsmitha Karunanithi 690a43be80fSAsmitha Karunanithi std::string headerLoc = 691a43be80fSAsmitha Karunanithi "Location: " + dumpPath + std::to_string(dumpId); 692002d39b4SEd Tanous taskData->payload->httpHeaders.emplace_back(std::move(headerLoc)); 693a43be80fSAsmitha Karunanithi 694a43be80fSAsmitha Karunanithi taskData->state = "Completed"; 695b47452b2SAsmitha Karunanithi return task::completed; 6966145ed6fSAsmitha Karunanithi } 697a43be80fSAsmitha Karunanithi return task::completed; 698a43be80fSAsmitha Karunanithi }, 6994978b63fSJason M. Bills "type='signal',interface='org.freedesktop.DBus.ObjectManager'," 700a43be80fSAsmitha Karunanithi "member='InterfacesAdded', " 701a43be80fSAsmitha Karunanithi "path='/xyz/openbmc_project/dump'"); 702a43be80fSAsmitha Karunanithi 703a43be80fSAsmitha Karunanithi task->startTimer(std::chrono::minutes(3)); 704a43be80fSAsmitha Karunanithi task->populateResp(asyncResp->res); 70598be3e39SEd Tanous task->payload.emplace(std::move(payload)); 706a43be80fSAsmitha Karunanithi } 707a43be80fSAsmitha Karunanithi 7088d1b46d7Szhanghch05 inline void createDump(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp, 7098d1b46d7Szhanghch05 const crow::Request& req, const std::string& dumpType) 710a43be80fSAsmitha Karunanithi { 711fdd26906SClaire Weinan std::string dumpPath = getDumpEntriesPath(dumpType); 712fdd26906SClaire Weinan if (dumpPath.empty()) 713a43be80fSAsmitha Karunanithi { 714a43be80fSAsmitha Karunanithi messages::internalError(asyncResp->res); 715a43be80fSAsmitha Karunanithi return; 716a43be80fSAsmitha Karunanithi } 717a43be80fSAsmitha Karunanithi 718a43be80fSAsmitha Karunanithi std::optional<std::string> diagnosticDataType; 719a43be80fSAsmitha Karunanithi std::optional<std::string> oemDiagnosticDataType; 720a43be80fSAsmitha Karunanithi 72115ed6780SWilly Tu if (!redfish::json_util::readJsonAction( 722a43be80fSAsmitha Karunanithi req, asyncResp->res, "DiagnosticDataType", diagnosticDataType, 723a43be80fSAsmitha Karunanithi "OEMDiagnosticDataType", oemDiagnosticDataType)) 724a43be80fSAsmitha Karunanithi { 725a43be80fSAsmitha Karunanithi return; 726a43be80fSAsmitha Karunanithi } 727a43be80fSAsmitha Karunanithi 728a43be80fSAsmitha Karunanithi if (dumpType == "System") 729a43be80fSAsmitha Karunanithi { 730a43be80fSAsmitha Karunanithi if (!oemDiagnosticDataType || !diagnosticDataType) 731a43be80fSAsmitha Karunanithi { 7324978b63fSJason M. Bills BMCWEB_LOG_ERROR 7334978b63fSJason M. Bills << "CreateDump action parameter 'DiagnosticDataType'/'OEMDiagnosticDataType' value not found!"; 734a43be80fSAsmitha Karunanithi messages::actionParameterMissing( 735a43be80fSAsmitha Karunanithi asyncResp->res, "CollectDiagnosticData", 736a43be80fSAsmitha Karunanithi "DiagnosticDataType & OEMDiagnosticDataType"); 737a43be80fSAsmitha Karunanithi return; 738a43be80fSAsmitha Karunanithi } 7393174e4dfSEd Tanous if ((*oemDiagnosticDataType != "System") || 740a43be80fSAsmitha Karunanithi (*diagnosticDataType != "OEM")) 741a43be80fSAsmitha Karunanithi { 742a43be80fSAsmitha Karunanithi BMCWEB_LOG_ERROR << "Wrong parameter values passed"; 743ace85d60SEd Tanous messages::internalError(asyncResp->res); 744a43be80fSAsmitha Karunanithi return; 745a43be80fSAsmitha Karunanithi } 7465907571dSAsmitha Karunanithi dumpPath = "/redfish/v1/Systems/system/LogServices/Dump/"; 747a43be80fSAsmitha Karunanithi } 748a43be80fSAsmitha Karunanithi else if (dumpType == "BMC") 749a43be80fSAsmitha Karunanithi { 750a43be80fSAsmitha Karunanithi if (!diagnosticDataType) 751a43be80fSAsmitha Karunanithi { 7520fda0f12SGeorge Liu BMCWEB_LOG_ERROR 7530fda0f12SGeorge Liu << "CreateDump action parameter 'DiagnosticDataType' not found!"; 754a43be80fSAsmitha Karunanithi messages::actionParameterMissing( 755a43be80fSAsmitha Karunanithi asyncResp->res, "CollectDiagnosticData", "DiagnosticDataType"); 756a43be80fSAsmitha Karunanithi return; 757a43be80fSAsmitha Karunanithi } 7583174e4dfSEd Tanous if (*diagnosticDataType != "Manager") 759a43be80fSAsmitha Karunanithi { 760a43be80fSAsmitha Karunanithi BMCWEB_LOG_ERROR 761a43be80fSAsmitha Karunanithi << "Wrong parameter value passed for 'DiagnosticDataType'"; 762ace85d60SEd Tanous messages::internalError(asyncResp->res); 763a43be80fSAsmitha Karunanithi return; 764a43be80fSAsmitha Karunanithi } 7655907571dSAsmitha Karunanithi dumpPath = "/redfish/v1/Managers/bmc/LogServices/Dump/"; 7665907571dSAsmitha Karunanithi } 7675907571dSAsmitha Karunanithi else 7685907571dSAsmitha Karunanithi { 7695907571dSAsmitha Karunanithi BMCWEB_LOG_ERROR << "CreateDump failed. Unknown dump type"; 7705907571dSAsmitha Karunanithi messages::internalError(asyncResp->res); 7715907571dSAsmitha Karunanithi return; 772a43be80fSAsmitha Karunanithi } 773a43be80fSAsmitha Karunanithi 774a43be80fSAsmitha Karunanithi crow::connections::systemBus->async_method_call( 77598be3e39SEd Tanous [asyncResp, payload(task::Payload(req)), dumpPath, 77698be3e39SEd Tanous dumpType](const boost::system::error_code ec, 7775907571dSAsmitha Karunanithi const sdbusplus::message::message& msg, 77898be3e39SEd Tanous const uint32_t& dumpId) mutable { 779a43be80fSAsmitha Karunanithi if (ec) 780a43be80fSAsmitha Karunanithi { 781a43be80fSAsmitha Karunanithi BMCWEB_LOG_ERROR << "CreateDump resp_handler got error " << ec; 7825907571dSAsmitha Karunanithi const sd_bus_error* dbusError = msg.get_error(); 7835907571dSAsmitha Karunanithi if (dbusError == nullptr) 7845907571dSAsmitha Karunanithi { 7855907571dSAsmitha Karunanithi messages::internalError(asyncResp->res); 7865907571dSAsmitha Karunanithi return; 7875907571dSAsmitha Karunanithi } 7885907571dSAsmitha Karunanithi 7895907571dSAsmitha Karunanithi BMCWEB_LOG_ERROR << "CreateDump DBus error: " << dbusError->name 7905907571dSAsmitha Karunanithi << " and error msg: " << dbusError->message; 7915907571dSAsmitha Karunanithi if (std::string_view( 7925907571dSAsmitha Karunanithi "xyz.openbmc_project.Common.Error.NotAllowed") == 7935907571dSAsmitha Karunanithi dbusError->name) 7945907571dSAsmitha Karunanithi { 7955907571dSAsmitha Karunanithi messages::resourceInStandby(asyncResp->res); 7965907571dSAsmitha Karunanithi return; 7975907571dSAsmitha Karunanithi } 7985907571dSAsmitha Karunanithi if (std::string_view( 7995907571dSAsmitha Karunanithi "xyz.openbmc_project.Dump.Create.Error.Disabled") == 8005907571dSAsmitha Karunanithi dbusError->name) 8015907571dSAsmitha Karunanithi { 8025907571dSAsmitha Karunanithi messages::serviceDisabled(asyncResp->res, dumpPath); 8035907571dSAsmitha Karunanithi return; 8045907571dSAsmitha Karunanithi } 8055907571dSAsmitha Karunanithi if (std::string_view( 8065907571dSAsmitha Karunanithi "xyz.openbmc_project.Common.Error.Unavailable") == 8075907571dSAsmitha Karunanithi dbusError->name) 8085907571dSAsmitha Karunanithi { 8095907571dSAsmitha Karunanithi messages::resourceInUse(asyncResp->res); 8105907571dSAsmitha Karunanithi return; 8115907571dSAsmitha Karunanithi } 8125907571dSAsmitha Karunanithi // Other Dbus errors such as: 8135907571dSAsmitha Karunanithi // xyz.openbmc_project.Common.Error.InvalidArgument & 8145907571dSAsmitha Karunanithi // org.freedesktop.DBus.Error.InvalidArgs are all related to 8155907571dSAsmitha Karunanithi // the dbus call that is made here in the bmcweb 8165907571dSAsmitha Karunanithi // implementation and has nothing to do with the client's 8175907571dSAsmitha Karunanithi // input in the request. Hence, returning internal error 8185907571dSAsmitha Karunanithi // back to the client. 819a43be80fSAsmitha Karunanithi messages::internalError(asyncResp->res); 820a43be80fSAsmitha Karunanithi return; 821a43be80fSAsmitha Karunanithi } 822a43be80fSAsmitha Karunanithi BMCWEB_LOG_DEBUG << "Dump Created. Id: " << dumpId; 823a43be80fSAsmitha Karunanithi 824002d39b4SEd Tanous createDumpTaskCallback(std::move(payload), asyncResp, dumpId, dumpPath, 825002d39b4SEd Tanous dumpType); 826a43be80fSAsmitha Karunanithi }, 827b47452b2SAsmitha Karunanithi "xyz.openbmc_project.Dump.Manager", 828b47452b2SAsmitha Karunanithi "/xyz/openbmc_project/dump/" + 829b47452b2SAsmitha Karunanithi std::string(boost::algorithm::to_lower_copy(dumpType)), 830a43be80fSAsmitha Karunanithi "xyz.openbmc_project.Dump.Create", "CreateDump"); 831a43be80fSAsmitha Karunanithi } 832a43be80fSAsmitha Karunanithi 8338d1b46d7Szhanghch05 inline void clearDump(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp, 8348d1b46d7Szhanghch05 const std::string& dumpType) 83580319af1SAsmitha Karunanithi { 836b47452b2SAsmitha Karunanithi std::string dumpTypeLowerCopy = 837b47452b2SAsmitha Karunanithi std::string(boost::algorithm::to_lower_copy(dumpType)); 8388d1b46d7Szhanghch05 83980319af1SAsmitha Karunanithi crow::connections::systemBus->async_method_call( 840b9d36b47SEd Tanous [asyncResp, dumpType]( 841b9d36b47SEd Tanous const boost::system::error_code ec, 842b9d36b47SEd Tanous const dbus::utility::MapperGetSubTreePathsResponse& subTreePaths) { 84380319af1SAsmitha Karunanithi if (ec) 84480319af1SAsmitha Karunanithi { 84580319af1SAsmitha Karunanithi BMCWEB_LOG_ERROR << "resp_handler got error " << ec; 84680319af1SAsmitha Karunanithi messages::internalError(asyncResp->res); 84780319af1SAsmitha Karunanithi return; 84880319af1SAsmitha Karunanithi } 84980319af1SAsmitha Karunanithi 85080319af1SAsmitha Karunanithi for (const std::string& path : subTreePaths) 85180319af1SAsmitha Karunanithi { 8522dfd18efSEd Tanous sdbusplus::message::object_path objPath(path); 8532dfd18efSEd Tanous std::string logID = objPath.filename(); 8542dfd18efSEd Tanous if (logID.empty()) 85580319af1SAsmitha Karunanithi { 8562dfd18efSEd Tanous continue; 85780319af1SAsmitha Karunanithi } 8582dfd18efSEd Tanous deleteDumpEntry(asyncResp, logID, dumpType); 85980319af1SAsmitha Karunanithi } 86080319af1SAsmitha Karunanithi }, 86180319af1SAsmitha Karunanithi "xyz.openbmc_project.ObjectMapper", 86280319af1SAsmitha Karunanithi "/xyz/openbmc_project/object_mapper", 86380319af1SAsmitha Karunanithi "xyz.openbmc_project.ObjectMapper", "GetSubTreePaths", 864b47452b2SAsmitha Karunanithi "/xyz/openbmc_project/dump/" + dumpTypeLowerCopy, 0, 865b47452b2SAsmitha Karunanithi std::array<std::string, 1>{"xyz.openbmc_project.Dump.Entry." + 866b47452b2SAsmitha Karunanithi dumpType}); 86780319af1SAsmitha Karunanithi } 86880319af1SAsmitha Karunanithi 869b9d36b47SEd Tanous inline static void 870b9d36b47SEd Tanous parseCrashdumpParameters(const dbus::utility::DBusPropertiesMap& params, 871b9d36b47SEd Tanous std::string& filename, std::string& timestamp, 872b9d36b47SEd Tanous std::string& logfile) 873043a0536SJohnathan Mantey { 874043a0536SJohnathan Mantey for (auto property : params) 875043a0536SJohnathan Mantey { 876043a0536SJohnathan Mantey if (property.first == "Timestamp") 877043a0536SJohnathan Mantey { 878043a0536SJohnathan Mantey const std::string* value = 8798d78b7a9SPatrick Williams std::get_if<std::string>(&property.second); 880043a0536SJohnathan Mantey if (value != nullptr) 881043a0536SJohnathan Mantey { 882043a0536SJohnathan Mantey timestamp = *value; 883043a0536SJohnathan Mantey } 884043a0536SJohnathan Mantey } 885043a0536SJohnathan Mantey else if (property.first == "Filename") 886043a0536SJohnathan Mantey { 887043a0536SJohnathan Mantey const std::string* value = 8888d78b7a9SPatrick Williams std::get_if<std::string>(&property.second); 889043a0536SJohnathan Mantey if (value != nullptr) 890043a0536SJohnathan Mantey { 891043a0536SJohnathan Mantey filename = *value; 892043a0536SJohnathan Mantey } 893043a0536SJohnathan Mantey } 894043a0536SJohnathan Mantey else if (property.first == "Log") 895043a0536SJohnathan Mantey { 896043a0536SJohnathan Mantey const std::string* value = 8978d78b7a9SPatrick Williams std::get_if<std::string>(&property.second); 898043a0536SJohnathan Mantey if (value != nullptr) 899043a0536SJohnathan Mantey { 900043a0536SJohnathan Mantey logfile = *value; 901043a0536SJohnathan Mantey } 902043a0536SJohnathan Mantey } 903043a0536SJohnathan Mantey } 904043a0536SJohnathan Mantey } 905043a0536SJohnathan Mantey 906a3316fc6SZhikuiRen constexpr char const* postCodeIface = "xyz.openbmc_project.State.Boot.PostCode"; 9077e860f15SJohn Edward Broadbent inline void requestRoutesSystemLogServiceCollection(App& app) 9081da66f75SEd Tanous { 909c4bf6374SJason M. Bills /** 910c4bf6374SJason M. Bills * Functions triggers appropriate requests on DBus 911c4bf6374SJason M. Bills */ 9127e860f15SJohn Edward Broadbent BMCWEB_ROUTE(app, "/redfish/v1/Systems/system/LogServices/") 913ed398213SEd Tanous .privileges(redfish::privileges::getLogServiceCollection) 914002d39b4SEd Tanous .methods(boost::beast::http::verb::get)( 915002d39b4SEd Tanous [&app](const crow::Request& req, 916002d39b4SEd Tanous const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) { 9173ba00073SCarson Labrado if (!redfish::setUpRedfishRoute(app, req, asyncResp)) 918c4bf6374SJason M. Bills { 91945ca1b86SEd Tanous return; 92045ca1b86SEd Tanous } 9217e860f15SJohn Edward Broadbent // Collections don't include the static data added by SubRoute 9227e860f15SJohn Edward Broadbent // because it has a duplicate entry for members 923c4bf6374SJason M. Bills asyncResp->res.jsonValue["@odata.type"] = 924c4bf6374SJason M. Bills "#LogServiceCollection.LogServiceCollection"; 925c4bf6374SJason M. Bills asyncResp->res.jsonValue["@odata.id"] = 926029573d4SEd Tanous "/redfish/v1/Systems/system/LogServices"; 92745ca1b86SEd Tanous asyncResp->res.jsonValue["Name"] = "System Log Services Collection"; 928c4bf6374SJason M. Bills asyncResp->res.jsonValue["Description"] = 929c4bf6374SJason M. Bills "Collection of LogServices for this Computer System"; 930002d39b4SEd Tanous nlohmann::json& logServiceArray = asyncResp->res.jsonValue["Members"]; 931c4bf6374SJason M. Bills logServiceArray = nlohmann::json::array(); 9321476687dSEd Tanous nlohmann::json::object_t eventLog; 9331476687dSEd Tanous eventLog["@odata.id"] = 9341476687dSEd Tanous "/redfish/v1/Systems/system/LogServices/EventLog"; 9351476687dSEd Tanous logServiceArray.push_back(std::move(eventLog)); 9365cb1dd27SAsmitha Karunanithi #ifdef BMCWEB_ENABLE_REDFISH_DUMP_LOG 9371476687dSEd Tanous nlohmann::json::object_t dumpLog; 938002d39b4SEd Tanous dumpLog["@odata.id"] = "/redfish/v1/Systems/system/LogServices/Dump"; 9391476687dSEd Tanous logServiceArray.push_back(std::move(dumpLog)); 940c9bb6861Sraviteja-b #endif 941c9bb6861Sraviteja-b 942d53dd41fSJason M. Bills #ifdef BMCWEB_ENABLE_REDFISH_CPU_LOG 9431476687dSEd Tanous nlohmann::json::object_t crashdump; 9441476687dSEd Tanous crashdump["@odata.id"] = 9451476687dSEd Tanous "/redfish/v1/Systems/system/LogServices/Crashdump"; 9461476687dSEd Tanous logServiceArray.push_back(std::move(crashdump)); 947d53dd41fSJason M. Bills #endif 948b7028ebfSSpencer Ku 949b7028ebfSSpencer Ku #ifdef BMCWEB_ENABLE_REDFISH_HOST_LOGGER 9501476687dSEd Tanous nlohmann::json::object_t hostlogger; 9511476687dSEd Tanous hostlogger["@odata.id"] = 9521476687dSEd Tanous "/redfish/v1/Systems/system/LogServices/HostLogger"; 9531476687dSEd Tanous logServiceArray.push_back(std::move(hostlogger)); 954b7028ebfSSpencer Ku #endif 955c4bf6374SJason M. Bills asyncResp->res.jsonValue["Members@odata.count"] = 956c4bf6374SJason M. Bills logServiceArray.size(); 957a3316fc6SZhikuiRen 958a3316fc6SZhikuiRen crow::connections::systemBus->async_method_call( 95945ca1b86SEd Tanous [asyncResp](const boost::system::error_code ec, 960b9d36b47SEd Tanous const dbus::utility::MapperGetSubTreePathsResponse& 961b9d36b47SEd Tanous subtreePath) { 962a3316fc6SZhikuiRen if (ec) 963a3316fc6SZhikuiRen { 964a3316fc6SZhikuiRen BMCWEB_LOG_ERROR << ec; 965a3316fc6SZhikuiRen return; 966a3316fc6SZhikuiRen } 967a3316fc6SZhikuiRen 96855f79e6fSEd Tanous for (const auto& pathStr : subtreePath) 969a3316fc6SZhikuiRen { 970a3316fc6SZhikuiRen if (pathStr.find("PostCode") != std::string::npos) 971a3316fc6SZhikuiRen { 97223a21a1cSEd Tanous nlohmann::json& logServiceArrayLocal = 973a3316fc6SZhikuiRen asyncResp->res.jsonValue["Members"]; 97423a21a1cSEd Tanous logServiceArrayLocal.push_back( 9750fda0f12SGeorge Liu {{"@odata.id", 9760fda0f12SGeorge Liu "/redfish/v1/Systems/system/LogServices/PostCodes"}}); 97745ca1b86SEd Tanous asyncResp->res.jsonValue["Members@odata.count"] = 97823a21a1cSEd Tanous logServiceArrayLocal.size(); 979a3316fc6SZhikuiRen return; 980a3316fc6SZhikuiRen } 981a3316fc6SZhikuiRen } 982a3316fc6SZhikuiRen }, 983a3316fc6SZhikuiRen "xyz.openbmc_project.ObjectMapper", 984a3316fc6SZhikuiRen "/xyz/openbmc_project/object_mapper", 98545ca1b86SEd Tanous "xyz.openbmc_project.ObjectMapper", "GetSubTreePaths", "/", 0, 98645ca1b86SEd Tanous std::array<const char*, 1>{postCodeIface}); 9877e860f15SJohn Edward Broadbent }); 988c4bf6374SJason M. Bills } 989c4bf6374SJason M. Bills 9907e860f15SJohn Edward Broadbent inline void requestRoutesEventLogService(App& app) 991c4bf6374SJason M. Bills { 9927e860f15SJohn Edward Broadbent BMCWEB_ROUTE(app, "/redfish/v1/Systems/system/LogServices/EventLog/") 993ed398213SEd Tanous .privileges(redfish::privileges::getLogService) 994002d39b4SEd Tanous .methods(boost::beast::http::verb::get)( 995002d39b4SEd Tanous [&app](const crow::Request& req, 996002d39b4SEd Tanous const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) { 9973ba00073SCarson Labrado if (!redfish::setUpRedfishRoute(app, req, asyncResp)) 99845ca1b86SEd Tanous { 99945ca1b86SEd Tanous return; 100045ca1b86SEd Tanous } 1001c4bf6374SJason M. Bills asyncResp->res.jsonValue["@odata.id"] = 1002029573d4SEd Tanous "/redfish/v1/Systems/system/LogServices/EventLog"; 1003c4bf6374SJason M. Bills asyncResp->res.jsonValue["@odata.type"] = 1004c4bf6374SJason M. Bills "#LogService.v1_1_0.LogService"; 1005c4bf6374SJason M. Bills asyncResp->res.jsonValue["Name"] = "Event Log Service"; 1006002d39b4SEd Tanous asyncResp->res.jsonValue["Description"] = "System Event Log Service"; 1007c4bf6374SJason M. Bills asyncResp->res.jsonValue["Id"] = "EventLog"; 1008c4bf6374SJason M. Bills asyncResp->res.jsonValue["OverWritePolicy"] = "WrapsWhenFull"; 10097c8c4058STejas Patil 10107c8c4058STejas Patil std::pair<std::string, std::string> redfishDateTimeOffset = 10112b82937eSEd Tanous redfish::time_utils::getDateTimeOffsetNow(); 10127c8c4058STejas Patil 10137c8c4058STejas Patil asyncResp->res.jsonValue["DateTime"] = redfishDateTimeOffset.first; 10147c8c4058STejas Patil asyncResp->res.jsonValue["DateTimeLocalOffset"] = 10157c8c4058STejas Patil redfishDateTimeOffset.second; 10167c8c4058STejas Patil 10171476687dSEd Tanous asyncResp->res.jsonValue["Entries"]["@odata.id"] = 10181476687dSEd Tanous "/redfish/v1/Systems/system/LogServices/EventLog/Entries"; 1019e7d6c8b2SGunnar Mills asyncResp->res.jsonValue["Actions"]["#LogService.ClearLog"] = { 1020e7d6c8b2SGunnar Mills 10210fda0f12SGeorge Liu {"target", 10220fda0f12SGeorge Liu "/redfish/v1/Systems/system/LogServices/EventLog/Actions/LogService.ClearLog"}}; 10237e860f15SJohn Edward Broadbent }); 1024489640c6SJason M. Bills } 1025489640c6SJason M. Bills 10267e860f15SJohn Edward Broadbent inline void requestRoutesJournalEventLogClear(App& app) 1027489640c6SJason M. Bills { 10284978b63fSJason M. Bills BMCWEB_ROUTE( 10294978b63fSJason M. Bills app, 10304978b63fSJason M. Bills "/redfish/v1/Systems/system/LogServices/EventLog/Actions/LogService.ClearLog/") 1031432a890cSEd Tanous .privileges({{"ConfigureComponents"}}) 10327e860f15SJohn Edward Broadbent .methods(boost::beast::http::verb::post)( 103345ca1b86SEd Tanous [&app](const crow::Request& req, 10347e860f15SJohn Edward Broadbent const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) { 10353ba00073SCarson Labrado if (!redfish::setUpRedfishRoute(app, req, asyncResp)) 103645ca1b86SEd Tanous { 103745ca1b86SEd Tanous return; 103845ca1b86SEd Tanous } 1039489640c6SJason M. Bills // Clear the EventLog by deleting the log files 1040489640c6SJason M. Bills std::vector<std::filesystem::path> redfishLogFiles; 1041489640c6SJason M. Bills if (getRedfishLogFiles(redfishLogFiles)) 1042489640c6SJason M. Bills { 1043489640c6SJason M. Bills for (const std::filesystem::path& file : redfishLogFiles) 1044489640c6SJason M. Bills { 1045489640c6SJason M. Bills std::error_code ec; 1046489640c6SJason M. Bills std::filesystem::remove(file, ec); 1047489640c6SJason M. Bills } 1048489640c6SJason M. Bills } 1049489640c6SJason M. Bills 1050489640c6SJason M. Bills // Reload rsyslog so it knows to start new log files 1051489640c6SJason M. Bills crow::connections::systemBus->async_method_call( 1052489640c6SJason M. Bills [asyncResp](const boost::system::error_code ec) { 1053489640c6SJason M. Bills if (ec) 1054489640c6SJason M. Bills { 1055002d39b4SEd Tanous BMCWEB_LOG_ERROR << "Failed to reload rsyslog: " << ec; 1056489640c6SJason M. Bills messages::internalError(asyncResp->res); 1057489640c6SJason M. Bills return; 1058489640c6SJason M. Bills } 1059489640c6SJason M. Bills 1060489640c6SJason M. Bills messages::success(asyncResp->res); 1061489640c6SJason M. Bills }, 1062489640c6SJason M. Bills "org.freedesktop.systemd1", "/org/freedesktop/systemd1", 1063002d39b4SEd Tanous "org.freedesktop.systemd1.Manager", "ReloadUnit", "rsyslog.service", 1064002d39b4SEd Tanous "replace"); 10657e860f15SJohn Edward Broadbent }); 1066c4bf6374SJason M. Bills } 1067c4bf6374SJason M. Bills 1068ac992cdeSJason M. Bills enum class LogParseError 1069ac992cdeSJason M. Bills { 1070ac992cdeSJason M. Bills success, 1071ac992cdeSJason M. Bills parseFailed, 1072ac992cdeSJason M. Bills messageIdNotInRegistry, 1073ac992cdeSJason M. Bills }; 1074ac992cdeSJason M. Bills 1075ac992cdeSJason M. Bills static LogParseError 1076ac992cdeSJason M. Bills fillEventLogEntryJson(const std::string& logEntryID, 1077b5a76932SEd Tanous const std::string& logEntry, 1078de703c5dSJason M. Bills nlohmann::json::object_t& logEntryJson) 1079c4bf6374SJason M. Bills { 108095820184SJason M. Bills // The redfish log format is "<Timestamp> <MessageId>,<MessageArgs>" 1081cd225da8SJason M. Bills // First get the Timestamp 1082f23b7296SEd Tanous size_t space = logEntry.find_first_of(' '); 1083cd225da8SJason M. Bills if (space == std::string::npos) 108495820184SJason M. Bills { 1085ac992cdeSJason M. Bills return LogParseError::parseFailed; 108695820184SJason M. Bills } 1087cd225da8SJason M. Bills std::string timestamp = logEntry.substr(0, space); 1088cd225da8SJason M. Bills // Then get the log contents 1089f23b7296SEd Tanous size_t entryStart = logEntry.find_first_not_of(' ', space); 1090cd225da8SJason M. Bills if (entryStart == std::string::npos) 1091cd225da8SJason M. Bills { 1092ac992cdeSJason M. Bills return LogParseError::parseFailed; 1093cd225da8SJason M. Bills } 1094cd225da8SJason M. Bills std::string_view entry(logEntry); 1095cd225da8SJason M. Bills entry.remove_prefix(entryStart); 1096cd225da8SJason M. Bills // Use split to separate the entry into its fields 1097cd225da8SJason M. Bills std::vector<std::string> logEntryFields; 1098cd225da8SJason M. Bills boost::split(logEntryFields, entry, boost::is_any_of(","), 1099cd225da8SJason M. Bills boost::token_compress_on); 1100cd225da8SJason M. Bills // We need at least a MessageId to be valid 110126f6976fSEd Tanous if (logEntryFields.empty()) 1102cd225da8SJason M. Bills { 1103ac992cdeSJason M. Bills return LogParseError::parseFailed; 1104cd225da8SJason M. Bills } 1105cd225da8SJason M. Bills std::string& messageID = logEntryFields[0]; 110695820184SJason M. Bills 11074851d45dSJason M. Bills // Get the Message from the MessageRegistry 1108fffb8c1fSEd Tanous const registries::Message* message = registries::getMessage(messageID); 1109c4bf6374SJason M. Bills 111054417b02SSui Chen if (message == nullptr) 1111c4bf6374SJason M. Bills { 111254417b02SSui Chen BMCWEB_LOG_WARNING << "Log entry not found in registry: " << logEntry; 1113ac992cdeSJason M. Bills return LogParseError::messageIdNotInRegistry; 1114c4bf6374SJason M. Bills } 1115c4bf6374SJason M. Bills 111654417b02SSui Chen std::string msg = message->message; 111754417b02SSui Chen 111815a86ff6SJason M. Bills // Get the MessageArgs from the log if there are any 111926702d01SEd Tanous std::span<std::string> messageArgs; 112015a86ff6SJason M. Bills if (logEntryFields.size() > 1) 112115a86ff6SJason M. Bills { 112215a86ff6SJason M. Bills std::string& messageArgsStart = logEntryFields[1]; 112315a86ff6SJason M. Bills // If the first string is empty, assume there are no MessageArgs 112415a86ff6SJason M. Bills std::size_t messageArgsSize = 0; 112515a86ff6SJason M. Bills if (!messageArgsStart.empty()) 112615a86ff6SJason M. Bills { 112715a86ff6SJason M. Bills messageArgsSize = logEntryFields.size() - 1; 112815a86ff6SJason M. Bills } 112915a86ff6SJason M. Bills 113023a21a1cSEd Tanous messageArgs = {&messageArgsStart, messageArgsSize}; 1131c4bf6374SJason M. Bills 11324851d45dSJason M. Bills // Fill the MessageArgs into the Message 113395820184SJason M. Bills int i = 0; 113495820184SJason M. Bills for (const std::string& messageArg : messageArgs) 11354851d45dSJason M. Bills { 113695820184SJason M. Bills std::string argStr = "%" + std::to_string(++i); 11374851d45dSJason M. Bills size_t argPos = msg.find(argStr); 11384851d45dSJason M. Bills if (argPos != std::string::npos) 11394851d45dSJason M. Bills { 114095820184SJason M. Bills msg.replace(argPos, argStr.length(), messageArg); 11414851d45dSJason M. Bills } 11424851d45dSJason M. Bills } 114315a86ff6SJason M. Bills } 11444851d45dSJason M. Bills 114595820184SJason M. Bills // Get the Created time from the timestamp. The log timestamp is in RFC3339 114695820184SJason M. Bills // format which matches the Redfish format except for the fractional seconds 114795820184SJason M. Bills // between the '.' and the '+', so just remove them. 1148f23b7296SEd Tanous std::size_t dot = timestamp.find_first_of('.'); 1149f23b7296SEd Tanous std::size_t plus = timestamp.find_first_of('+'); 115095820184SJason M. Bills if (dot != std::string::npos && plus != std::string::npos) 1151c4bf6374SJason M. Bills { 115295820184SJason M. Bills timestamp.erase(dot, plus - dot); 1153c4bf6374SJason M. Bills } 1154c4bf6374SJason M. Bills 1155c4bf6374SJason M. Bills // Fill in the log entry with the gathered data 115684afc48bSJason M. Bills logEntryJson["@odata.type"] = "#LogEntry.v1_8_0.LogEntry"; 115784afc48bSJason M. Bills logEntryJson["@odata.id"] = 115884afc48bSJason M. Bills "/redfish/v1/Systems/system/LogServices/EventLog/Entries/" + logEntryID; 115984afc48bSJason M. Bills logEntryJson["Name"] = "System Event Log Entry"; 116084afc48bSJason M. Bills logEntryJson["Id"] = logEntryID; 116184afc48bSJason M. Bills logEntryJson["Message"] = std::move(msg); 116284afc48bSJason M. Bills logEntryJson["MessageId"] = std::move(messageID); 116384afc48bSJason M. Bills logEntryJson["MessageArgs"] = messageArgs; 116484afc48bSJason M. Bills logEntryJson["EntryType"] = "Event"; 116584afc48bSJason M. Bills logEntryJson["Severity"] = message->messageSeverity; 116684afc48bSJason M. Bills logEntryJson["Created"] = std::move(timestamp); 1167ac992cdeSJason M. Bills return LogParseError::success; 1168c4bf6374SJason M. Bills } 1169c4bf6374SJason M. Bills 11707e860f15SJohn Edward Broadbent inline void requestRoutesJournalEventLogEntryCollection(App& app) 1171c4bf6374SJason M. Bills { 11727e860f15SJohn Edward Broadbent BMCWEB_ROUTE(app, 11737e860f15SJohn Edward Broadbent "/redfish/v1/Systems/system/LogServices/EventLog/Entries/") 11748b6a35f0SGunnar Mills .privileges(redfish::privileges::getLogEntryCollection) 1175002d39b4SEd Tanous .methods(boost::beast::http::verb::get)( 1176002d39b4SEd Tanous [&app](const crow::Request& req, 1177002d39b4SEd Tanous const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) { 1178c937d2bfSEd Tanous query_param::QueryCapabilities capabilities = { 1179c937d2bfSEd Tanous .canDelegateTop = true, 1180c937d2bfSEd Tanous .canDelegateSkip = true, 1181c937d2bfSEd Tanous }; 1182c937d2bfSEd Tanous query_param::Query delegatedQuery; 1183c937d2bfSEd Tanous if (!redfish::setUpRedfishRouteWithDelegation( 11843ba00073SCarson Labrado app, req, asyncResp, delegatedQuery, capabilities)) 1185c4bf6374SJason M. Bills { 1186c4bf6374SJason M. Bills return; 1187c4bf6374SJason M. Bills } 11885143f7a5SJiaqing Zhao size_t top = delegatedQuery.top.value_or(query_param::Query::maxTop); 11893648c8beSEd Tanous size_t skip = delegatedQuery.skip.value_or(0); 11903648c8beSEd Tanous 11917e860f15SJohn Edward Broadbent // Collections don't include the static data added by SubRoute 11927e860f15SJohn Edward Broadbent // because it has a duplicate entry for members 1193c4bf6374SJason M. Bills asyncResp->res.jsonValue["@odata.type"] = 1194c4bf6374SJason M. Bills "#LogEntryCollection.LogEntryCollection"; 1195c4bf6374SJason M. Bills asyncResp->res.jsonValue["@odata.id"] = 1196029573d4SEd Tanous "/redfish/v1/Systems/system/LogServices/EventLog/Entries"; 1197c4bf6374SJason M. Bills asyncResp->res.jsonValue["Name"] = "System Event Log Entries"; 1198c4bf6374SJason M. Bills asyncResp->res.jsonValue["Description"] = 1199c4bf6374SJason M. Bills "Collection of System Event Log Entries"; 1200cb92c03bSAndrew Geissler 12014978b63fSJason M. Bills nlohmann::json& logEntryArray = asyncResp->res.jsonValue["Members"]; 1202c4bf6374SJason M. Bills logEntryArray = nlohmann::json::array(); 12037e860f15SJohn Edward Broadbent // Go through the log files and create a unique ID for each 12047e860f15SJohn Edward Broadbent // entry 120595820184SJason M. Bills std::vector<std::filesystem::path> redfishLogFiles; 120695820184SJason M. Bills getRedfishLogFiles(redfishLogFiles); 1207b01bf299SEd Tanous uint64_t entryCount = 0; 1208cd225da8SJason M. Bills std::string logEntry; 120995820184SJason M. Bills 12107e860f15SJohn Edward Broadbent // Oldest logs are in the last file, so start there and loop 12117e860f15SJohn Edward Broadbent // backwards 1212002d39b4SEd Tanous for (auto it = redfishLogFiles.rbegin(); it < redfishLogFiles.rend(); 1213002d39b4SEd Tanous it++) 1214c4bf6374SJason M. Bills { 1215cd225da8SJason M. Bills std::ifstream logStream(*it); 121695820184SJason M. Bills if (!logStream.is_open()) 1217c4bf6374SJason M. Bills { 1218c4bf6374SJason M. Bills continue; 1219c4bf6374SJason M. Bills } 1220c4bf6374SJason M. Bills 1221e85d6b16SJason M. Bills // Reset the unique ID on the first entry 1222e85d6b16SJason M. Bills bool firstEntry = true; 122395820184SJason M. Bills while (std::getline(logStream, logEntry)) 122495820184SJason M. Bills { 1225c4bf6374SJason M. Bills std::string idStr; 1226e85d6b16SJason M. Bills if (!getUniqueEntryID(logEntry, idStr, firstEntry)) 1227c4bf6374SJason M. Bills { 1228c4bf6374SJason M. Bills continue; 1229c4bf6374SJason M. Bills } 1230e85d6b16SJason M. Bills firstEntry = false; 1231e85d6b16SJason M. Bills 1232de703c5dSJason M. Bills nlohmann::json::object_t bmcLogEntry; 1233ac992cdeSJason M. Bills LogParseError status = 1234ac992cdeSJason M. Bills fillEventLogEntryJson(idStr, logEntry, bmcLogEntry); 1235ac992cdeSJason M. Bills if (status == LogParseError::messageIdNotInRegistry) 1236ac992cdeSJason M. Bills { 1237ac992cdeSJason M. Bills continue; 1238ac992cdeSJason M. Bills } 1239ac992cdeSJason M. Bills if (status != LogParseError::success) 1240c4bf6374SJason M. Bills { 1241c4bf6374SJason M. Bills messages::internalError(asyncResp->res); 1242c4bf6374SJason M. Bills return; 1243c4bf6374SJason M. Bills } 1244de703c5dSJason M. Bills 1245de703c5dSJason M. Bills entryCount++; 1246de703c5dSJason M. Bills // Handle paging using skip (number of entries to skip from the 1247de703c5dSJason M. Bills // start) and top (number of entries to display) 12483648c8beSEd Tanous if (entryCount <= skip || entryCount > skip + top) 1249de703c5dSJason M. Bills { 1250de703c5dSJason M. Bills continue; 1251de703c5dSJason M. Bills } 1252de703c5dSJason M. Bills 1253de703c5dSJason M. Bills logEntryArray.push_back(std::move(bmcLogEntry)); 1254c4bf6374SJason M. Bills } 125595820184SJason M. Bills } 1256c4bf6374SJason M. Bills asyncResp->res.jsonValue["Members@odata.count"] = entryCount; 12573648c8beSEd Tanous if (skip + top < entryCount) 1258c4bf6374SJason M. Bills { 1259c4bf6374SJason M. Bills asyncResp->res.jsonValue["Members@odata.nextLink"] = 12604978b63fSJason M. Bills "/redfish/v1/Systems/system/LogServices/EventLog/Entries?$skip=" + 12613648c8beSEd Tanous std::to_string(skip + top); 1262c4bf6374SJason M. Bills } 12637e860f15SJohn Edward Broadbent }); 1264897967deSJason M. Bills } 1265897967deSJason M. Bills 12667e860f15SJohn Edward Broadbent inline void requestRoutesJournalEventLogEntry(App& app) 1267897967deSJason M. Bills { 12687e860f15SJohn Edward Broadbent BMCWEB_ROUTE( 12697e860f15SJohn Edward Broadbent app, "/redfish/v1/Systems/system/LogServices/EventLog/Entries/<str>/") 1270ed398213SEd Tanous .privileges(redfish::privileges::getLogEntry) 12717e860f15SJohn Edward Broadbent .methods(boost::beast::http::verb::get)( 127245ca1b86SEd Tanous [&app](const crow::Request& req, 12737e860f15SJohn Edward Broadbent const std::shared_ptr<bmcweb::AsyncResp>& asyncResp, 12747e860f15SJohn Edward Broadbent const std::string& param) { 12753ba00073SCarson Labrado if (!redfish::setUpRedfishRoute(app, req, asyncResp)) 127645ca1b86SEd Tanous { 127745ca1b86SEd Tanous return; 127845ca1b86SEd Tanous } 12797e860f15SJohn Edward Broadbent const std::string& targetID = param; 12808d1b46d7Szhanghch05 12817e860f15SJohn Edward Broadbent // Go through the log files and check the unique ID for each 12827e860f15SJohn Edward Broadbent // entry to find the target entry 1283897967deSJason M. Bills std::vector<std::filesystem::path> redfishLogFiles; 1284897967deSJason M. Bills getRedfishLogFiles(redfishLogFiles); 1285897967deSJason M. Bills std::string logEntry; 1286897967deSJason M. Bills 12877e860f15SJohn Edward Broadbent // Oldest logs are in the last file, so start there and loop 12887e860f15SJohn Edward Broadbent // backwards 1289002d39b4SEd Tanous for (auto it = redfishLogFiles.rbegin(); it < redfishLogFiles.rend(); 1290002d39b4SEd Tanous it++) 1291897967deSJason M. Bills { 1292897967deSJason M. Bills std::ifstream logStream(*it); 1293897967deSJason M. Bills if (!logStream.is_open()) 1294897967deSJason M. Bills { 1295897967deSJason M. Bills continue; 1296897967deSJason M. Bills } 1297897967deSJason M. Bills 1298897967deSJason M. Bills // Reset the unique ID on the first entry 1299897967deSJason M. Bills bool firstEntry = true; 1300897967deSJason M. Bills while (std::getline(logStream, logEntry)) 1301897967deSJason M. Bills { 1302897967deSJason M. Bills std::string idStr; 1303897967deSJason M. Bills if (!getUniqueEntryID(logEntry, idStr, firstEntry)) 1304897967deSJason M. Bills { 1305897967deSJason M. Bills continue; 1306897967deSJason M. Bills } 1307897967deSJason M. Bills firstEntry = false; 1308897967deSJason M. Bills 1309897967deSJason M. Bills if (idStr == targetID) 1310897967deSJason M. Bills { 1311de703c5dSJason M. Bills nlohmann::json::object_t bmcLogEntry; 1312ac992cdeSJason M. Bills LogParseError status = 1313ac992cdeSJason M. Bills fillEventLogEntryJson(idStr, logEntry, bmcLogEntry); 1314ac992cdeSJason M. Bills if (status != LogParseError::success) 1315897967deSJason M. Bills { 1316897967deSJason M. Bills messages::internalError(asyncResp->res); 1317897967deSJason M. Bills return; 1318897967deSJason M. Bills } 1319d405bb51SJason M. Bills asyncResp->res.jsonValue.update(bmcLogEntry); 1320897967deSJason M. Bills return; 1321897967deSJason M. Bills } 1322897967deSJason M. Bills } 1323897967deSJason M. Bills } 1324897967deSJason M. Bills // Requested ID was not found 1325002d39b4SEd Tanous messages::resourceMissingAtURI(asyncResp->res, 1326002d39b4SEd Tanous crow::utility::urlFromPieces(targetID)); 13277e860f15SJohn Edward Broadbent }); 132808a4e4b5SAnthony Wilson } 132908a4e4b5SAnthony Wilson 13307e860f15SJohn Edward Broadbent inline void requestRoutesDBusEventLogEntryCollection(App& app) 133108a4e4b5SAnthony Wilson { 13327e860f15SJohn Edward Broadbent BMCWEB_ROUTE(app, 13337e860f15SJohn Edward Broadbent "/redfish/v1/Systems/system/LogServices/EventLog/Entries/") 1334ed398213SEd Tanous .privileges(redfish::privileges::getLogEntryCollection) 1335002d39b4SEd Tanous .methods(boost::beast::http::verb::get)( 1336002d39b4SEd Tanous [&app](const crow::Request& req, 1337002d39b4SEd Tanous const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) { 13383ba00073SCarson Labrado if (!redfish::setUpRedfishRoute(app, req, asyncResp)) 133945ca1b86SEd Tanous { 134045ca1b86SEd Tanous return; 134145ca1b86SEd Tanous } 13427e860f15SJohn Edward Broadbent // Collections don't include the static data added by SubRoute 13437e860f15SJohn Edward Broadbent // because it has a duplicate entry for members 134408a4e4b5SAnthony Wilson asyncResp->res.jsonValue["@odata.type"] = 134508a4e4b5SAnthony Wilson "#LogEntryCollection.LogEntryCollection"; 134608a4e4b5SAnthony Wilson asyncResp->res.jsonValue["@odata.id"] = 134708a4e4b5SAnthony Wilson "/redfish/v1/Systems/system/LogServices/EventLog/Entries"; 134808a4e4b5SAnthony Wilson asyncResp->res.jsonValue["Name"] = "System Event Log Entries"; 134908a4e4b5SAnthony Wilson asyncResp->res.jsonValue["Description"] = 135008a4e4b5SAnthony Wilson "Collection of System Event Log Entries"; 135108a4e4b5SAnthony Wilson 1352cb92c03bSAndrew Geissler // DBus implementation of EventLog/Entries 1353cb92c03bSAndrew Geissler // Make call to Logging Service to find all log entry objects 1354cb92c03bSAndrew Geissler crow::connections::systemBus->async_method_call( 1355cb92c03bSAndrew Geissler [asyncResp](const boost::system::error_code ec, 1356914e2d5dSEd Tanous const dbus::utility::ManagedObjectType& resp) { 1357cb92c03bSAndrew Geissler if (ec) 1358cb92c03bSAndrew Geissler { 1359cb92c03bSAndrew Geissler // TODO Handle for specific error code 1360cb92c03bSAndrew Geissler BMCWEB_LOG_ERROR 1361002d39b4SEd Tanous << "getLogEntriesIfaceData resp_handler got error " << ec; 1362cb92c03bSAndrew Geissler messages::internalError(asyncResp->res); 1363cb92c03bSAndrew Geissler return; 1364cb92c03bSAndrew Geissler } 1365002d39b4SEd Tanous nlohmann::json& entriesArray = asyncResp->res.jsonValue["Members"]; 1366cb92c03bSAndrew Geissler entriesArray = nlohmann::json::array(); 13679eb808c1SEd Tanous for (const auto& objectPath : resp) 1368cb92c03bSAndrew Geissler { 1369914e2d5dSEd Tanous const uint32_t* id = nullptr; 1370c419c759SEd Tanous const uint64_t* timestamp = nullptr; 1371c419c759SEd Tanous const uint64_t* updateTimestamp = nullptr; 1372914e2d5dSEd Tanous const std::string* severity = nullptr; 1373914e2d5dSEd Tanous const std::string* message = nullptr; 1374914e2d5dSEd Tanous const std::string* filePath = nullptr; 137575710de2SXiaochao Ma bool resolved = false; 13769eb808c1SEd Tanous for (const auto& interfaceMap : objectPath.second) 1377f86bb901SAdriana Kobylak { 1378f86bb901SAdriana Kobylak if (interfaceMap.first == 1379f86bb901SAdriana Kobylak "xyz.openbmc_project.Logging.Entry") 1380f86bb901SAdriana Kobylak { 1381002d39b4SEd Tanous for (const auto& propertyMap : interfaceMap.second) 1382cb92c03bSAndrew Geissler { 1383cb92c03bSAndrew Geissler if (propertyMap.first == "Id") 1384cb92c03bSAndrew Geissler { 1385002d39b4SEd Tanous id = std::get_if<uint32_t>(&propertyMap.second); 1386cb92c03bSAndrew Geissler } 1387cb92c03bSAndrew Geissler else if (propertyMap.first == "Timestamp") 1388cb92c03bSAndrew Geissler { 1389002d39b4SEd Tanous timestamp = 1390002d39b4SEd Tanous std::get_if<uint64_t>(&propertyMap.second); 13917e860f15SJohn Edward Broadbent } 1392002d39b4SEd Tanous else if (propertyMap.first == "UpdateTimestamp") 13937e860f15SJohn Edward Broadbent { 1394002d39b4SEd Tanous updateTimestamp = 1395002d39b4SEd Tanous std::get_if<uint64_t>(&propertyMap.second); 13967e860f15SJohn Edward Broadbent } 13977e860f15SJohn Edward Broadbent else if (propertyMap.first == "Severity") 13987e860f15SJohn Edward Broadbent { 13997e860f15SJohn Edward Broadbent severity = std::get_if<std::string>( 14007e860f15SJohn Edward Broadbent &propertyMap.second); 14017e860f15SJohn Edward Broadbent } 14027e860f15SJohn Edward Broadbent else if (propertyMap.first == "Message") 14037e860f15SJohn Edward Broadbent { 14047e860f15SJohn Edward Broadbent message = std::get_if<std::string>( 14057e860f15SJohn Edward Broadbent &propertyMap.second); 14067e860f15SJohn Edward Broadbent } 14077e860f15SJohn Edward Broadbent else if (propertyMap.first == "Resolved") 14087e860f15SJohn Edward Broadbent { 1409914e2d5dSEd Tanous const bool* resolveptr = 1410002d39b4SEd Tanous std::get_if<bool>(&propertyMap.second); 14117e860f15SJohn Edward Broadbent if (resolveptr == nullptr) 14127e860f15SJohn Edward Broadbent { 1413002d39b4SEd Tanous messages::internalError(asyncResp->res); 14147e860f15SJohn Edward Broadbent return; 14157e860f15SJohn Edward Broadbent } 14167e860f15SJohn Edward Broadbent resolved = *resolveptr; 14177e860f15SJohn Edward Broadbent } 14187e860f15SJohn Edward Broadbent } 14197e860f15SJohn Edward Broadbent if (id == nullptr || message == nullptr || 14207e860f15SJohn Edward Broadbent severity == nullptr) 14217e860f15SJohn Edward Broadbent { 14227e860f15SJohn Edward Broadbent messages::internalError(asyncResp->res); 14237e860f15SJohn Edward Broadbent return; 14247e860f15SJohn Edward Broadbent } 14257e860f15SJohn Edward Broadbent } 14267e860f15SJohn Edward Broadbent else if (interfaceMap.first == 14277e860f15SJohn Edward Broadbent "xyz.openbmc_project.Common.FilePath") 14287e860f15SJohn Edward Broadbent { 1429002d39b4SEd Tanous for (const auto& propertyMap : interfaceMap.second) 14307e860f15SJohn Edward Broadbent { 14317e860f15SJohn Edward Broadbent if (propertyMap.first == "Path") 14327e860f15SJohn Edward Broadbent { 14337e860f15SJohn Edward Broadbent filePath = std::get_if<std::string>( 14347e860f15SJohn Edward Broadbent &propertyMap.second); 14357e860f15SJohn Edward Broadbent } 14367e860f15SJohn Edward Broadbent } 14377e860f15SJohn Edward Broadbent } 14387e860f15SJohn Edward Broadbent } 14397e860f15SJohn Edward Broadbent // Object path without the 14407e860f15SJohn Edward Broadbent // xyz.openbmc_project.Logging.Entry interface, ignore 14417e860f15SJohn Edward Broadbent // and continue. 14427e860f15SJohn Edward Broadbent if (id == nullptr || message == nullptr || 1443c419c759SEd Tanous severity == nullptr || timestamp == nullptr || 1444c419c759SEd Tanous updateTimestamp == nullptr) 14457e860f15SJohn Edward Broadbent { 14467e860f15SJohn Edward Broadbent continue; 14477e860f15SJohn Edward Broadbent } 14487e860f15SJohn Edward Broadbent entriesArray.push_back({}); 14497e860f15SJohn Edward Broadbent nlohmann::json& thisEntry = entriesArray.back(); 14507e860f15SJohn Edward Broadbent thisEntry["@odata.type"] = "#LogEntry.v1_8_0.LogEntry"; 14517e860f15SJohn Edward Broadbent thisEntry["@odata.id"] = 14520fda0f12SGeorge Liu "/redfish/v1/Systems/system/LogServices/EventLog/Entries/" + 14537e860f15SJohn Edward Broadbent std::to_string(*id); 14547e860f15SJohn Edward Broadbent thisEntry["Name"] = "System Event Log Entry"; 14557e860f15SJohn Edward Broadbent thisEntry["Id"] = std::to_string(*id); 14567e860f15SJohn Edward Broadbent thisEntry["Message"] = *message; 14577e860f15SJohn Edward Broadbent thisEntry["Resolved"] = resolved; 14587e860f15SJohn Edward Broadbent thisEntry["EntryType"] = "Event"; 14597e860f15SJohn Edward Broadbent thisEntry["Severity"] = 14607e860f15SJohn Edward Broadbent translateSeverityDbusToRedfish(*severity); 14617e860f15SJohn Edward Broadbent thisEntry["Created"] = 14622b82937eSEd Tanous redfish::time_utils::getDateTimeUintMs(*timestamp); 14637e860f15SJohn Edward Broadbent thisEntry["Modified"] = 14642b82937eSEd Tanous redfish::time_utils::getDateTimeUintMs(*updateTimestamp); 14657e860f15SJohn Edward Broadbent if (filePath != nullptr) 14667e860f15SJohn Edward Broadbent { 14677e860f15SJohn Edward Broadbent thisEntry["AdditionalDataURI"] = 14680fda0f12SGeorge Liu "/redfish/v1/Systems/system/LogServices/EventLog/Entries/" + 14697e860f15SJohn Edward Broadbent std::to_string(*id) + "/attachment"; 14707e860f15SJohn Edward Broadbent } 14717e860f15SJohn Edward Broadbent } 1472002d39b4SEd Tanous std::sort( 1473002d39b4SEd Tanous entriesArray.begin(), entriesArray.end(), 1474002d39b4SEd Tanous [](const nlohmann::json& left, const nlohmann::json& right) { 14757e860f15SJohn Edward Broadbent return (left["Id"] <= right["Id"]); 14767e860f15SJohn Edward Broadbent }); 14777e860f15SJohn Edward Broadbent asyncResp->res.jsonValue["Members@odata.count"] = 14787e860f15SJohn Edward Broadbent entriesArray.size(); 14797e860f15SJohn Edward Broadbent }, 14807e860f15SJohn Edward Broadbent "xyz.openbmc_project.Logging", "/xyz/openbmc_project/logging", 14817e860f15SJohn Edward Broadbent "org.freedesktop.DBus.ObjectManager", "GetManagedObjects"); 14827e860f15SJohn Edward Broadbent }); 14837e860f15SJohn Edward Broadbent } 14847e860f15SJohn Edward Broadbent 14857e860f15SJohn Edward Broadbent inline void requestRoutesDBusEventLogEntry(App& app) 14867e860f15SJohn Edward Broadbent { 14877e860f15SJohn Edward Broadbent BMCWEB_ROUTE( 14887e860f15SJohn Edward Broadbent app, "/redfish/v1/Systems/system/LogServices/EventLog/Entries/<str>/") 1489ed398213SEd Tanous .privileges(redfish::privileges::getLogEntry) 1490002d39b4SEd Tanous .methods(boost::beast::http::verb::get)( 1491002d39b4SEd Tanous [&app](const crow::Request& req, 14927e860f15SJohn Edward Broadbent const std::shared_ptr<bmcweb::AsyncResp>& asyncResp, 149345ca1b86SEd Tanous const std::string& param) { 14943ba00073SCarson Labrado if (!redfish::setUpRedfishRoute(app, req, asyncResp)) 14957e860f15SJohn Edward Broadbent { 149645ca1b86SEd Tanous return; 149745ca1b86SEd Tanous } 14987e860f15SJohn Edward Broadbent std::string entryID = param; 14997e860f15SJohn Edward Broadbent dbus::utility::escapePathForDbus(entryID); 15007e860f15SJohn Edward Broadbent 15017e860f15SJohn Edward Broadbent // DBus implementation of EventLog/Entries 15027e860f15SJohn Edward Broadbent // Make call to Logging Service to find all log entry objects 15037e860f15SJohn Edward Broadbent crow::connections::systemBus->async_method_call( 1504002d39b4SEd Tanous [asyncResp, entryID](const boost::system::error_code ec, 1505b9d36b47SEd Tanous const dbus::utility::DBusPropertiesMap& resp) { 15067e860f15SJohn Edward Broadbent if (ec.value() == EBADR) 15077e860f15SJohn Edward Broadbent { 1508d8a5d5d8SJiaqing Zhao messages::resourceNotFound(asyncResp->res, "LogEntry", entryID); 15097e860f15SJohn Edward Broadbent return; 15107e860f15SJohn Edward Broadbent } 15117e860f15SJohn Edward Broadbent if (ec) 15127e860f15SJohn Edward Broadbent { 15130fda0f12SGeorge Liu BMCWEB_LOG_ERROR 1514002d39b4SEd Tanous << "EventLogEntry (DBus) resp_handler got error " << ec; 15157e860f15SJohn Edward Broadbent messages::internalError(asyncResp->res); 15167e860f15SJohn Edward Broadbent return; 15177e860f15SJohn Edward Broadbent } 1518914e2d5dSEd Tanous const uint32_t* id = nullptr; 1519c419c759SEd Tanous const uint64_t* timestamp = nullptr; 1520c419c759SEd Tanous const uint64_t* updateTimestamp = nullptr; 1521914e2d5dSEd Tanous const std::string* severity = nullptr; 1522914e2d5dSEd Tanous const std::string* message = nullptr; 1523914e2d5dSEd Tanous const std::string* filePath = nullptr; 15247e860f15SJohn Edward Broadbent bool resolved = false; 15257e860f15SJohn Edward Broadbent 15269eb808c1SEd Tanous for (const auto& propertyMap : resp) 15277e860f15SJohn Edward Broadbent { 15287e860f15SJohn Edward Broadbent if (propertyMap.first == "Id") 15297e860f15SJohn Edward Broadbent { 15307e860f15SJohn Edward Broadbent id = std::get_if<uint32_t>(&propertyMap.second); 15317e860f15SJohn Edward Broadbent } 15327e860f15SJohn Edward Broadbent else if (propertyMap.first == "Timestamp") 15337e860f15SJohn Edward Broadbent { 1534002d39b4SEd Tanous timestamp = std::get_if<uint64_t>(&propertyMap.second); 1535ebd45906SGeorge Liu } 1536d139c236SGeorge Liu else if (propertyMap.first == "UpdateTimestamp") 1537d139c236SGeorge Liu { 1538ebd45906SGeorge Liu updateTimestamp = 1539c419c759SEd Tanous std::get_if<uint64_t>(&propertyMap.second); 1540ebd45906SGeorge Liu } 1541cb92c03bSAndrew Geissler else if (propertyMap.first == "Severity") 1542cb92c03bSAndrew Geissler { 1543002d39b4SEd Tanous severity = std::get_if<std::string>(&propertyMap.second); 1544cb92c03bSAndrew Geissler } 1545cb92c03bSAndrew Geissler else if (propertyMap.first == "Message") 1546cb92c03bSAndrew Geissler { 1547002d39b4SEd Tanous message = std::get_if<std::string>(&propertyMap.second); 1548ae34c8e8SAdriana Kobylak } 154975710de2SXiaochao Ma else if (propertyMap.first == "Resolved") 155075710de2SXiaochao Ma { 1551914e2d5dSEd Tanous const bool* resolveptr = 155275710de2SXiaochao Ma std::get_if<bool>(&propertyMap.second); 155375710de2SXiaochao Ma if (resolveptr == nullptr) 155475710de2SXiaochao Ma { 155575710de2SXiaochao Ma messages::internalError(asyncResp->res); 155675710de2SXiaochao Ma return; 155775710de2SXiaochao Ma } 155875710de2SXiaochao Ma resolved = *resolveptr; 155975710de2SXiaochao Ma } 15607e860f15SJohn Edward Broadbent else if (propertyMap.first == "Path") 1561f86bb901SAdriana Kobylak { 1562002d39b4SEd Tanous filePath = std::get_if<std::string>(&propertyMap.second); 1563f86bb901SAdriana Kobylak } 1564f86bb901SAdriana Kobylak } 1565002d39b4SEd Tanous if (id == nullptr || message == nullptr || severity == nullptr || 1566002d39b4SEd Tanous timestamp == nullptr || updateTimestamp == nullptr) 1567f86bb901SAdriana Kobylak { 1568ae34c8e8SAdriana Kobylak messages::internalError(asyncResp->res); 1569271584abSEd Tanous return; 1570271584abSEd Tanous } 1571f86bb901SAdriana Kobylak asyncResp->res.jsonValue["@odata.type"] = 1572f86bb901SAdriana Kobylak "#LogEntry.v1_8_0.LogEntry"; 1573f86bb901SAdriana Kobylak asyncResp->res.jsonValue["@odata.id"] = 15740fda0f12SGeorge Liu "/redfish/v1/Systems/system/LogServices/EventLog/Entries/" + 1575f86bb901SAdriana Kobylak std::to_string(*id); 157645ca1b86SEd Tanous asyncResp->res.jsonValue["Name"] = "System Event Log Entry"; 1577f86bb901SAdriana Kobylak asyncResp->res.jsonValue["Id"] = std::to_string(*id); 1578f86bb901SAdriana Kobylak asyncResp->res.jsonValue["Message"] = *message; 1579f86bb901SAdriana Kobylak asyncResp->res.jsonValue["Resolved"] = resolved; 1580f86bb901SAdriana Kobylak asyncResp->res.jsonValue["EntryType"] = "Event"; 1581f86bb901SAdriana Kobylak asyncResp->res.jsonValue["Severity"] = 1582f86bb901SAdriana Kobylak translateSeverityDbusToRedfish(*severity); 1583f86bb901SAdriana Kobylak asyncResp->res.jsonValue["Created"] = 15842b82937eSEd Tanous redfish::time_utils::getDateTimeUintMs(*timestamp); 1585f86bb901SAdriana Kobylak asyncResp->res.jsonValue["Modified"] = 15862b82937eSEd Tanous redfish::time_utils::getDateTimeUintMs(*updateTimestamp); 1587f86bb901SAdriana Kobylak if (filePath != nullptr) 1588f86bb901SAdriana Kobylak { 1589f86bb901SAdriana Kobylak asyncResp->res.jsonValue["AdditionalDataURI"] = 1590e7dbd530SPotin Lai "/redfish/v1/Systems/system/LogServices/EventLog/Entries/" + 1591e7dbd530SPotin Lai std::to_string(*id) + "/attachment"; 1592f86bb901SAdriana Kobylak } 1593cb92c03bSAndrew Geissler }, 1594cb92c03bSAndrew Geissler "xyz.openbmc_project.Logging", 1595cb92c03bSAndrew Geissler "/xyz/openbmc_project/logging/entry/" + entryID, 1596f86bb901SAdriana Kobylak "org.freedesktop.DBus.Properties", "GetAll", ""); 15977e860f15SJohn Edward Broadbent }); 1598336e96c6SChicago Duan 15997e860f15SJohn Edward Broadbent BMCWEB_ROUTE( 16007e860f15SJohn Edward Broadbent app, "/redfish/v1/Systems/system/LogServices/EventLog/Entries/<str>/") 1601ed398213SEd Tanous .privileges(redfish::privileges::patchLogEntry) 16027e860f15SJohn Edward Broadbent .methods(boost::beast::http::verb::patch)( 160345ca1b86SEd Tanous [&app](const crow::Request& req, 16047e860f15SJohn Edward Broadbent const std::shared_ptr<bmcweb::AsyncResp>& asyncResp, 16057e860f15SJohn Edward Broadbent const std::string& entryId) { 16063ba00073SCarson Labrado if (!redfish::setUpRedfishRoute(app, req, asyncResp)) 160745ca1b86SEd Tanous { 160845ca1b86SEd Tanous return; 160945ca1b86SEd Tanous } 161075710de2SXiaochao Ma std::optional<bool> resolved; 161175710de2SXiaochao Ma 161215ed6780SWilly Tu if (!json_util::readJsonPatch(req, asyncResp->res, "Resolved", 16137e860f15SJohn Edward Broadbent resolved)) 161475710de2SXiaochao Ma { 161575710de2SXiaochao Ma return; 161675710de2SXiaochao Ma } 161775710de2SXiaochao Ma BMCWEB_LOG_DEBUG << "Set Resolved"; 161875710de2SXiaochao Ma 161975710de2SXiaochao Ma crow::connections::systemBus->async_method_call( 16204f48d5f6SEd Tanous [asyncResp, entryId](const boost::system::error_code ec) { 162175710de2SXiaochao Ma if (ec) 162275710de2SXiaochao Ma { 162375710de2SXiaochao Ma BMCWEB_LOG_DEBUG << "DBUS response error " << ec; 162475710de2SXiaochao Ma messages::internalError(asyncResp->res); 162575710de2SXiaochao Ma return; 162675710de2SXiaochao Ma } 162775710de2SXiaochao Ma }, 162875710de2SXiaochao Ma "xyz.openbmc_project.Logging", 162975710de2SXiaochao Ma "/xyz/openbmc_project/logging/entry/" + entryId, 163075710de2SXiaochao Ma "org.freedesktop.DBus.Properties", "Set", 163175710de2SXiaochao Ma "xyz.openbmc_project.Logging.Entry", "Resolved", 1632168e20c1SEd Tanous dbus::utility::DbusVariantType(*resolved)); 16337e860f15SJohn Edward Broadbent }); 163475710de2SXiaochao Ma 16357e860f15SJohn Edward Broadbent BMCWEB_ROUTE( 16367e860f15SJohn Edward Broadbent app, "/redfish/v1/Systems/system/LogServices/EventLog/Entries/<str>/") 1637ed398213SEd Tanous .privileges(redfish::privileges::deleteLogEntry) 1638ed398213SEd Tanous 1639002d39b4SEd Tanous .methods(boost::beast::http::verb::delete_)( 1640002d39b4SEd Tanous [&app](const crow::Request& req, 1641002d39b4SEd Tanous const std::shared_ptr<bmcweb::AsyncResp>& asyncResp, 164245ca1b86SEd Tanous const std::string& param) { 16433ba00073SCarson Labrado if (!redfish::setUpRedfishRoute(app, req, asyncResp)) 1644336e96c6SChicago Duan { 164545ca1b86SEd Tanous return; 164645ca1b86SEd Tanous } 1647336e96c6SChicago Duan BMCWEB_LOG_DEBUG << "Do delete single event entries."; 1648336e96c6SChicago Duan 16497e860f15SJohn Edward Broadbent std::string entryID = param; 1650336e96c6SChicago Duan 1651336e96c6SChicago Duan dbus::utility::escapePathForDbus(entryID); 1652336e96c6SChicago Duan 1653336e96c6SChicago Duan // Process response from Logging service. 1654002d39b4SEd Tanous auto respHandler = 1655002d39b4SEd Tanous [asyncResp, entryID](const boost::system::error_code ec) { 1656002d39b4SEd Tanous BMCWEB_LOG_DEBUG << "EventLogEntry (DBus) doDelete callback: Done"; 1657336e96c6SChicago Duan if (ec) 1658336e96c6SChicago Duan { 16593de8d8baSGeorge Liu if (ec.value() == EBADR) 16603de8d8baSGeorge Liu { 166145ca1b86SEd Tanous messages::resourceNotFound(asyncResp->res, "LogEntry", 166245ca1b86SEd Tanous entryID); 16633de8d8baSGeorge Liu return; 16643de8d8baSGeorge Liu } 1665336e96c6SChicago Duan // TODO Handle for specific error code 16660fda0f12SGeorge Liu BMCWEB_LOG_ERROR 16670fda0f12SGeorge Liu << "EventLogEntry (DBus) doDelete respHandler got error " 1668336e96c6SChicago Duan << ec; 1669336e96c6SChicago Duan asyncResp->res.result( 1670336e96c6SChicago Duan boost::beast::http::status::internal_server_error); 1671336e96c6SChicago Duan return; 1672336e96c6SChicago Duan } 1673336e96c6SChicago Duan 1674336e96c6SChicago Duan asyncResp->res.result(boost::beast::http::status::ok); 1675336e96c6SChicago Duan }; 1676336e96c6SChicago Duan 1677336e96c6SChicago Duan // Make call to Logging service to request Delete Log 1678336e96c6SChicago Duan crow::connections::systemBus->async_method_call( 1679336e96c6SChicago Duan respHandler, "xyz.openbmc_project.Logging", 1680336e96c6SChicago Duan "/xyz/openbmc_project/logging/entry/" + entryID, 1681336e96c6SChicago Duan "xyz.openbmc_project.Object.Delete", "Delete"); 16827e860f15SJohn Edward Broadbent }); 1683400fd1fbSAdriana Kobylak } 1684400fd1fbSAdriana Kobylak 16857e860f15SJohn Edward Broadbent inline void requestRoutesDBusEventLogEntryDownload(App& app) 1686400fd1fbSAdriana Kobylak { 16870fda0f12SGeorge Liu BMCWEB_ROUTE( 16880fda0f12SGeorge Liu app, 16890fda0f12SGeorge Liu "/redfish/v1/Systems/system/LogServices/EventLog/Entries/<str>/attachment") 1690ed398213SEd Tanous .privileges(redfish::privileges::getLogEntry) 16917e860f15SJohn Edward Broadbent .methods(boost::beast::http::verb::get)( 169245ca1b86SEd Tanous [&app](const crow::Request& req, 16937e860f15SJohn Edward Broadbent const std::shared_ptr<bmcweb::AsyncResp>& asyncResp, 169445ca1b86SEd Tanous const std::string& param) { 16953ba00073SCarson Labrado if (!redfish::setUpRedfishRoute(app, req, asyncResp)) 16967e860f15SJohn Edward Broadbent { 169745ca1b86SEd Tanous return; 169845ca1b86SEd Tanous } 1699002d39b4SEd Tanous if (!http_helpers::isOctetAccepted(req.getHeaderValue("Accept"))) 1700400fd1fbSAdriana Kobylak { 1701002d39b4SEd Tanous asyncResp->res.result(boost::beast::http::status::bad_request); 1702400fd1fbSAdriana Kobylak return; 1703400fd1fbSAdriana Kobylak } 1704400fd1fbSAdriana Kobylak 17057e860f15SJohn Edward Broadbent std::string entryID = param; 1706400fd1fbSAdriana Kobylak dbus::utility::escapePathForDbus(entryID); 1707400fd1fbSAdriana Kobylak 1708400fd1fbSAdriana Kobylak crow::connections::systemBus->async_method_call( 1709002d39b4SEd Tanous [asyncResp, entryID](const boost::system::error_code ec, 1710400fd1fbSAdriana Kobylak const sdbusplus::message::unix_fd& unixfd) { 1711400fd1fbSAdriana Kobylak if (ec.value() == EBADR) 1712400fd1fbSAdriana Kobylak { 1713002d39b4SEd Tanous messages::resourceNotFound(asyncResp->res, "EventLogAttachment", 1714002d39b4SEd Tanous entryID); 1715400fd1fbSAdriana Kobylak return; 1716400fd1fbSAdriana Kobylak } 1717400fd1fbSAdriana Kobylak if (ec) 1718400fd1fbSAdriana Kobylak { 1719400fd1fbSAdriana Kobylak BMCWEB_LOG_DEBUG << "DBUS response error " << ec; 1720400fd1fbSAdriana Kobylak messages::internalError(asyncResp->res); 1721400fd1fbSAdriana Kobylak return; 1722400fd1fbSAdriana Kobylak } 1723400fd1fbSAdriana Kobylak 1724400fd1fbSAdriana Kobylak int fd = -1; 1725400fd1fbSAdriana Kobylak fd = dup(unixfd); 1726400fd1fbSAdriana Kobylak if (fd == -1) 1727400fd1fbSAdriana Kobylak { 1728400fd1fbSAdriana Kobylak messages::internalError(asyncResp->res); 1729400fd1fbSAdriana Kobylak return; 1730400fd1fbSAdriana Kobylak } 1731400fd1fbSAdriana Kobylak 1732400fd1fbSAdriana Kobylak long long int size = lseek(fd, 0, SEEK_END); 1733400fd1fbSAdriana Kobylak if (size == -1) 1734400fd1fbSAdriana Kobylak { 1735400fd1fbSAdriana Kobylak messages::internalError(asyncResp->res); 1736400fd1fbSAdriana Kobylak return; 1737400fd1fbSAdriana Kobylak } 1738400fd1fbSAdriana Kobylak 1739400fd1fbSAdriana Kobylak // Arbitrary max size of 64kb 1740400fd1fbSAdriana Kobylak constexpr int maxFileSize = 65536; 1741400fd1fbSAdriana Kobylak if (size > maxFileSize) 1742400fd1fbSAdriana Kobylak { 1743002d39b4SEd Tanous BMCWEB_LOG_ERROR << "File size exceeds maximum allowed size of " 1744400fd1fbSAdriana Kobylak << maxFileSize; 1745400fd1fbSAdriana Kobylak messages::internalError(asyncResp->res); 1746400fd1fbSAdriana Kobylak return; 1747400fd1fbSAdriana Kobylak } 1748400fd1fbSAdriana Kobylak std::vector<char> data(static_cast<size_t>(size)); 1749400fd1fbSAdriana Kobylak long long int rc = lseek(fd, 0, SEEK_SET); 1750400fd1fbSAdriana Kobylak if (rc == -1) 1751400fd1fbSAdriana Kobylak { 1752400fd1fbSAdriana Kobylak messages::internalError(asyncResp->res); 1753400fd1fbSAdriana Kobylak return; 1754400fd1fbSAdriana Kobylak } 1755400fd1fbSAdriana Kobylak rc = read(fd, data.data(), data.size()); 1756400fd1fbSAdriana Kobylak if ((rc == -1) || (rc != size)) 1757400fd1fbSAdriana Kobylak { 1758400fd1fbSAdriana Kobylak messages::internalError(asyncResp->res); 1759400fd1fbSAdriana Kobylak return; 1760400fd1fbSAdriana Kobylak } 1761400fd1fbSAdriana Kobylak close(fd); 1762400fd1fbSAdriana Kobylak 1763400fd1fbSAdriana Kobylak std::string_view strData(data.data(), data.size()); 1764002d39b4SEd Tanous std::string output = crow::utility::base64encode(strData); 1765400fd1fbSAdriana Kobylak 1766d9f6c621SEd Tanous asyncResp->res.addHeader(boost::beast::http::field::content_type, 1767400fd1fbSAdriana Kobylak "application/octet-stream"); 1768d9f6c621SEd Tanous asyncResp->res.addHeader( 1769d9f6c621SEd Tanous boost::beast::http::field::content_transfer_encoding, "Base64"); 1770400fd1fbSAdriana Kobylak asyncResp->res.body() = std::move(output); 1771400fd1fbSAdriana Kobylak }, 1772400fd1fbSAdriana Kobylak "xyz.openbmc_project.Logging", 1773400fd1fbSAdriana Kobylak "/xyz/openbmc_project/logging/entry/" + entryID, 1774400fd1fbSAdriana Kobylak "xyz.openbmc_project.Logging.Entry", "GetEntry"); 17757e860f15SJohn Edward Broadbent }); 17761da66f75SEd Tanous } 17771da66f75SEd Tanous 1778b7028ebfSSpencer Ku constexpr const char* hostLoggerFolderPath = "/var/log/console"; 1779b7028ebfSSpencer Ku 1780b7028ebfSSpencer Ku inline bool 1781b7028ebfSSpencer Ku getHostLoggerFiles(const std::string& hostLoggerFilePath, 1782b7028ebfSSpencer Ku std::vector<std::filesystem::path>& hostLoggerFiles) 1783b7028ebfSSpencer Ku { 1784b7028ebfSSpencer Ku std::error_code ec; 1785b7028ebfSSpencer Ku std::filesystem::directory_iterator logPath(hostLoggerFilePath, ec); 1786b7028ebfSSpencer Ku if (ec) 1787b7028ebfSSpencer Ku { 1788b7028ebfSSpencer Ku BMCWEB_LOG_ERROR << ec.message(); 1789b7028ebfSSpencer Ku return false; 1790b7028ebfSSpencer Ku } 1791b7028ebfSSpencer Ku for (const std::filesystem::directory_entry& it : logPath) 1792b7028ebfSSpencer Ku { 1793b7028ebfSSpencer Ku std::string filename = it.path().filename(); 1794b7028ebfSSpencer Ku // Prefix of each log files is "log". Find the file and save the 1795b7028ebfSSpencer Ku // path 179611ba3979SEd Tanous if (filename.starts_with("log")) 1797b7028ebfSSpencer Ku { 1798b7028ebfSSpencer Ku hostLoggerFiles.emplace_back(it.path()); 1799b7028ebfSSpencer Ku } 1800b7028ebfSSpencer Ku } 1801b7028ebfSSpencer Ku // As the log files rotate, they are appended with a ".#" that is higher for 1802b7028ebfSSpencer Ku // the older logs. Since we start from oldest logs, sort the name in 1803b7028ebfSSpencer Ku // descending order. 1804b7028ebfSSpencer Ku std::sort(hostLoggerFiles.rbegin(), hostLoggerFiles.rend(), 1805b7028ebfSSpencer Ku AlphanumLess<std::string>()); 1806b7028ebfSSpencer Ku 1807b7028ebfSSpencer Ku return true; 1808b7028ebfSSpencer Ku } 1809b7028ebfSSpencer Ku 181002cad96eSEd Tanous inline bool getHostLoggerEntries( 181102cad96eSEd Tanous const std::vector<std::filesystem::path>& hostLoggerFiles, uint64_t skip, 181202cad96eSEd Tanous uint64_t top, std::vector<std::string>& logEntries, size_t& logCount) 1813b7028ebfSSpencer Ku { 1814b7028ebfSSpencer Ku GzFileReader logFile; 1815b7028ebfSSpencer Ku 1816b7028ebfSSpencer Ku // Go though all log files and expose host logs. 1817b7028ebfSSpencer Ku for (const std::filesystem::path& it : hostLoggerFiles) 1818b7028ebfSSpencer Ku { 1819b7028ebfSSpencer Ku if (!logFile.gzGetLines(it.string(), skip, top, logEntries, logCount)) 1820b7028ebfSSpencer Ku { 1821b7028ebfSSpencer Ku BMCWEB_LOG_ERROR << "fail to expose host logs"; 1822b7028ebfSSpencer Ku return false; 1823b7028ebfSSpencer Ku } 1824b7028ebfSSpencer Ku } 1825b7028ebfSSpencer Ku // Get lastMessage from constructor by getter 1826b7028ebfSSpencer Ku std::string lastMessage = logFile.getLastMessage(); 1827b7028ebfSSpencer Ku if (!lastMessage.empty()) 1828b7028ebfSSpencer Ku { 1829b7028ebfSSpencer Ku logCount++; 1830b7028ebfSSpencer Ku if (logCount > skip && logCount <= (skip + top)) 1831b7028ebfSSpencer Ku { 1832b7028ebfSSpencer Ku logEntries.push_back(lastMessage); 1833b7028ebfSSpencer Ku } 1834b7028ebfSSpencer Ku } 1835b7028ebfSSpencer Ku return true; 1836b7028ebfSSpencer Ku } 1837b7028ebfSSpencer Ku 1838b7028ebfSSpencer Ku inline void fillHostLoggerEntryJson(const std::string& logEntryID, 1839b7028ebfSSpencer Ku const std::string& msg, 18406d6574c9SJason M. Bills nlohmann::json::object_t& logEntryJson) 1841b7028ebfSSpencer Ku { 1842b7028ebfSSpencer Ku // Fill in the log entry with the gathered data. 18436d6574c9SJason M. Bills logEntryJson["@odata.type"] = "#LogEntry.v1_4_0.LogEntry"; 18446d6574c9SJason M. Bills logEntryJson["@odata.id"] = 1845b7028ebfSSpencer Ku "/redfish/v1/Systems/system/LogServices/HostLogger/Entries/" + 18466d6574c9SJason M. Bills logEntryID; 18476d6574c9SJason M. Bills logEntryJson["Name"] = "Host Logger Entry"; 18486d6574c9SJason M. Bills logEntryJson["Id"] = logEntryID; 18496d6574c9SJason M. Bills logEntryJson["Message"] = msg; 18506d6574c9SJason M. Bills logEntryJson["EntryType"] = "Oem"; 18516d6574c9SJason M. Bills logEntryJson["Severity"] = "OK"; 18526d6574c9SJason M. Bills logEntryJson["OemRecordFormat"] = "Host Logger Entry"; 1853b7028ebfSSpencer Ku } 1854b7028ebfSSpencer Ku 1855b7028ebfSSpencer Ku inline void requestRoutesSystemHostLogger(App& app) 1856b7028ebfSSpencer Ku { 1857b7028ebfSSpencer Ku BMCWEB_ROUTE(app, "/redfish/v1/Systems/system/LogServices/HostLogger/") 1858b7028ebfSSpencer Ku .privileges(redfish::privileges::getLogService) 18591476687dSEd Tanous .methods(boost::beast::http::verb::get)( 18601476687dSEd Tanous [&app](const crow::Request& req, 18611476687dSEd Tanous const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) { 18623ba00073SCarson Labrado if (!redfish::setUpRedfishRoute(app, req, asyncResp)) 186345ca1b86SEd Tanous { 186445ca1b86SEd Tanous return; 186545ca1b86SEd Tanous } 1866b7028ebfSSpencer Ku asyncResp->res.jsonValue["@odata.id"] = 1867b7028ebfSSpencer Ku "/redfish/v1/Systems/system/LogServices/HostLogger"; 1868b7028ebfSSpencer Ku asyncResp->res.jsonValue["@odata.type"] = 1869b7028ebfSSpencer Ku "#LogService.v1_1_0.LogService"; 1870b7028ebfSSpencer Ku asyncResp->res.jsonValue["Name"] = "Host Logger Service"; 1871b7028ebfSSpencer Ku asyncResp->res.jsonValue["Description"] = "Host Logger Service"; 1872b7028ebfSSpencer Ku asyncResp->res.jsonValue["Id"] = "HostLogger"; 18731476687dSEd Tanous asyncResp->res.jsonValue["Entries"]["@odata.id"] = 18741476687dSEd Tanous "/redfish/v1/Systems/system/LogServices/HostLogger/Entries"; 1875b7028ebfSSpencer Ku }); 1876b7028ebfSSpencer Ku } 1877b7028ebfSSpencer Ku 1878b7028ebfSSpencer Ku inline void requestRoutesSystemHostLoggerCollection(App& app) 1879b7028ebfSSpencer Ku { 1880b7028ebfSSpencer Ku BMCWEB_ROUTE(app, 1881b7028ebfSSpencer Ku "/redfish/v1/Systems/system/LogServices/HostLogger/Entries/") 1882b7028ebfSSpencer Ku .privileges(redfish::privileges::getLogEntry) 1883002d39b4SEd Tanous .methods(boost::beast::http::verb::get)( 1884002d39b4SEd Tanous [&app](const crow::Request& req, 1885002d39b4SEd Tanous const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) { 1886c937d2bfSEd Tanous query_param::QueryCapabilities capabilities = { 1887c937d2bfSEd Tanous .canDelegateTop = true, 1888c937d2bfSEd Tanous .canDelegateSkip = true, 1889c937d2bfSEd Tanous }; 1890c937d2bfSEd Tanous query_param::Query delegatedQuery; 1891c937d2bfSEd Tanous if (!redfish::setUpRedfishRouteWithDelegation( 18923ba00073SCarson Labrado app, req, asyncResp, delegatedQuery, capabilities)) 1893b7028ebfSSpencer Ku { 1894b7028ebfSSpencer Ku return; 1895b7028ebfSSpencer Ku } 1896b7028ebfSSpencer Ku asyncResp->res.jsonValue["@odata.id"] = 1897b7028ebfSSpencer Ku "/redfish/v1/Systems/system/LogServices/HostLogger/Entries"; 1898b7028ebfSSpencer Ku asyncResp->res.jsonValue["@odata.type"] = 1899b7028ebfSSpencer Ku "#LogEntryCollection.LogEntryCollection"; 1900b7028ebfSSpencer Ku asyncResp->res.jsonValue["Name"] = "HostLogger Entries"; 1901b7028ebfSSpencer Ku asyncResp->res.jsonValue["Description"] = 1902b7028ebfSSpencer Ku "Collection of HostLogger Entries"; 19030fda0f12SGeorge Liu nlohmann::json& logEntryArray = asyncResp->res.jsonValue["Members"]; 1904b7028ebfSSpencer Ku logEntryArray = nlohmann::json::array(); 1905b7028ebfSSpencer Ku asyncResp->res.jsonValue["Members@odata.count"] = 0; 1906b7028ebfSSpencer Ku 1907b7028ebfSSpencer Ku std::vector<std::filesystem::path> hostLoggerFiles; 1908b7028ebfSSpencer Ku if (!getHostLoggerFiles(hostLoggerFolderPath, hostLoggerFiles)) 1909b7028ebfSSpencer Ku { 1910b7028ebfSSpencer Ku BMCWEB_LOG_ERROR << "fail to get host log file path"; 1911b7028ebfSSpencer Ku return; 1912b7028ebfSSpencer Ku } 19133648c8beSEd Tanous // If we weren't provided top and skip limits, use the defaults. 19143648c8beSEd Tanous size_t skip = delegatedQuery.skip.value_or(0); 19155143f7a5SJiaqing Zhao size_t top = delegatedQuery.top.value_or(query_param::Query::maxTop); 1916b7028ebfSSpencer Ku size_t logCount = 0; 1917b7028ebfSSpencer Ku // This vector only store the entries we want to expose that 1918b7028ebfSSpencer Ku // control by skip and top. 1919b7028ebfSSpencer Ku std::vector<std::string> logEntries; 19203648c8beSEd Tanous if (!getHostLoggerEntries(hostLoggerFiles, skip, top, logEntries, 19213648c8beSEd Tanous logCount)) 1922b7028ebfSSpencer Ku { 1923b7028ebfSSpencer Ku messages::internalError(asyncResp->res); 1924b7028ebfSSpencer Ku return; 1925b7028ebfSSpencer Ku } 1926b7028ebfSSpencer Ku // If vector is empty, that means skip value larger than total 1927b7028ebfSSpencer Ku // log count 192826f6976fSEd Tanous if (logEntries.empty()) 1929b7028ebfSSpencer Ku { 1930b7028ebfSSpencer Ku asyncResp->res.jsonValue["Members@odata.count"] = logCount; 1931b7028ebfSSpencer Ku return; 1932b7028ebfSSpencer Ku } 193326f6976fSEd Tanous if (!logEntries.empty()) 1934b7028ebfSSpencer Ku { 1935b7028ebfSSpencer Ku for (size_t i = 0; i < logEntries.size(); i++) 1936b7028ebfSSpencer Ku { 19376d6574c9SJason M. Bills nlohmann::json::object_t hostLogEntry; 19383648c8beSEd Tanous fillHostLoggerEntryJson(std::to_string(skip + i), logEntries[i], 19393648c8beSEd Tanous hostLogEntry); 19406d6574c9SJason M. Bills logEntryArray.push_back(std::move(hostLogEntry)); 1941b7028ebfSSpencer Ku } 1942b7028ebfSSpencer Ku 1943b7028ebfSSpencer Ku asyncResp->res.jsonValue["Members@odata.count"] = logCount; 19443648c8beSEd Tanous if (skip + top < logCount) 1945b7028ebfSSpencer Ku { 1946b7028ebfSSpencer Ku asyncResp->res.jsonValue["Members@odata.nextLink"] = 19470fda0f12SGeorge Liu "/redfish/v1/Systems/system/LogServices/HostLogger/Entries?$skip=" + 19483648c8beSEd Tanous std::to_string(skip + top); 1949b7028ebfSSpencer Ku } 1950b7028ebfSSpencer Ku } 1951b7028ebfSSpencer Ku }); 1952b7028ebfSSpencer Ku } 1953b7028ebfSSpencer Ku 1954b7028ebfSSpencer Ku inline void requestRoutesSystemHostLoggerLogEntry(App& app) 1955b7028ebfSSpencer Ku { 1956b7028ebfSSpencer Ku BMCWEB_ROUTE( 1957b7028ebfSSpencer Ku app, "/redfish/v1/Systems/system/LogServices/HostLogger/Entries/<str>/") 1958b7028ebfSSpencer Ku .privileges(redfish::privileges::getLogEntry) 1959b7028ebfSSpencer Ku .methods(boost::beast::http::verb::get)( 196045ca1b86SEd Tanous [&app](const crow::Request& req, 1961b7028ebfSSpencer Ku const std::shared_ptr<bmcweb::AsyncResp>& asyncResp, 1962b7028ebfSSpencer Ku const std::string& param) { 19633ba00073SCarson Labrado if (!redfish::setUpRedfishRoute(app, req, asyncResp)) 196445ca1b86SEd Tanous { 196545ca1b86SEd Tanous return; 196645ca1b86SEd Tanous } 1967b7028ebfSSpencer Ku const std::string& targetID = param; 1968b7028ebfSSpencer Ku 1969b7028ebfSSpencer Ku uint64_t idInt = 0; 1970ca45aa3cSEd Tanous 1971ca45aa3cSEd Tanous // NOLINTNEXTLINE(cppcoreguidelines-pro-bounds-pointer-arithmetic) 1972ca45aa3cSEd Tanous const char* end = targetID.data() + targetID.size(); 1973ca45aa3cSEd Tanous 1974ca45aa3cSEd Tanous auto [ptr, ec] = std::from_chars(targetID.data(), end, idInt); 1975b7028ebfSSpencer Ku if (ec == std::errc::invalid_argument) 1976b7028ebfSSpencer Ku { 1977ace85d60SEd Tanous messages::resourceMissingAtURI(asyncResp->res, req.urlView); 1978b7028ebfSSpencer Ku return; 1979b7028ebfSSpencer Ku } 1980b7028ebfSSpencer Ku if (ec == std::errc::result_out_of_range) 1981b7028ebfSSpencer Ku { 1982ace85d60SEd Tanous messages::resourceMissingAtURI(asyncResp->res, req.urlView); 1983b7028ebfSSpencer Ku return; 1984b7028ebfSSpencer Ku } 1985b7028ebfSSpencer Ku 1986b7028ebfSSpencer Ku std::vector<std::filesystem::path> hostLoggerFiles; 1987b7028ebfSSpencer Ku if (!getHostLoggerFiles(hostLoggerFolderPath, hostLoggerFiles)) 1988b7028ebfSSpencer Ku { 1989b7028ebfSSpencer Ku BMCWEB_LOG_ERROR << "fail to get host log file path"; 1990b7028ebfSSpencer Ku return; 1991b7028ebfSSpencer Ku } 1992b7028ebfSSpencer Ku 1993b7028ebfSSpencer Ku size_t logCount = 0; 19943648c8beSEd Tanous size_t top = 1; 1995b7028ebfSSpencer Ku std::vector<std::string> logEntries; 1996b7028ebfSSpencer Ku // We can get specific entry by skip and top. For example, if we 1997b7028ebfSSpencer Ku // want to get nth entry, we can set skip = n-1 and top = 1 to 1998b7028ebfSSpencer Ku // get that entry 1999002d39b4SEd Tanous if (!getHostLoggerEntries(hostLoggerFiles, idInt, top, logEntries, 2000002d39b4SEd Tanous logCount)) 2001b7028ebfSSpencer Ku { 2002b7028ebfSSpencer Ku messages::internalError(asyncResp->res); 2003b7028ebfSSpencer Ku return; 2004b7028ebfSSpencer Ku } 2005b7028ebfSSpencer Ku 2006b7028ebfSSpencer Ku if (!logEntries.empty()) 2007b7028ebfSSpencer Ku { 20086d6574c9SJason M. Bills nlohmann::json::object_t hostLogEntry; 20096d6574c9SJason M. Bills fillHostLoggerEntryJson(targetID, logEntries[0], hostLogEntry); 20106d6574c9SJason M. Bills asyncResp->res.jsonValue.update(hostLogEntry); 2011b7028ebfSSpencer Ku return; 2012b7028ebfSSpencer Ku } 2013b7028ebfSSpencer Ku 2014b7028ebfSSpencer Ku // Requested ID was not found 2015ace85d60SEd Tanous messages::resourceMissingAtURI(asyncResp->res, req.urlView); 2016b7028ebfSSpencer Ku }); 2017b7028ebfSSpencer Ku } 2018b7028ebfSSpencer Ku 2019fdd26906SClaire Weinan constexpr char const* dumpManagerIface = 2020fdd26906SClaire Weinan "xyz.openbmc_project.Collection.DeleteAll"; 2021dd72e87bSClaire Weinan inline void handleBMCLogServicesCollectionGet( 2022fdd26906SClaire Weinan crow::App& app, const crow::Request& req, 2023fdd26906SClaire Weinan const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) 20241da66f75SEd Tanous { 20253ba00073SCarson Labrado if (!redfish::setUpRedfishRoute(app, req, asyncResp)) 202645ca1b86SEd Tanous { 202745ca1b86SEd Tanous return; 202845ca1b86SEd Tanous } 20297e860f15SJohn Edward Broadbent // Collections don't include the static data added by SubRoute 20307e860f15SJohn Edward Broadbent // because it has a duplicate entry for members 2031e1f26343SJason M. Bills asyncResp->res.jsonValue["@odata.type"] = 20321da66f75SEd Tanous "#LogServiceCollection.LogServiceCollection"; 2033e1f26343SJason M. Bills asyncResp->res.jsonValue["@odata.id"] = 2034e1f26343SJason M. Bills "/redfish/v1/Managers/bmc/LogServices"; 2035002d39b4SEd Tanous asyncResp->res.jsonValue["Name"] = "Open BMC Log Services Collection"; 2036e1f26343SJason M. Bills asyncResp->res.jsonValue["Description"] = 20371da66f75SEd Tanous "Collection of LogServices for this Manager"; 2038002d39b4SEd Tanous nlohmann::json& logServiceArray = asyncResp->res.jsonValue["Members"]; 2039c4bf6374SJason M. Bills logServiceArray = nlohmann::json::array(); 2040fdd26906SClaire Weinan 2041c4bf6374SJason M. Bills #ifdef BMCWEB_ENABLE_REDFISH_BMC_JOURNAL 2042c4bf6374SJason M. Bills logServiceArray.push_back( 2043002d39b4SEd Tanous {{"@odata.id", "/redfish/v1/Managers/bmc/LogServices/Journal"}}); 2044c4bf6374SJason M. Bills #endif 2045fdd26906SClaire Weinan 2046fdd26906SClaire Weinan asyncResp->res.jsonValue["Members@odata.count"] = logServiceArray.size(); 2047fdd26906SClaire Weinan 2048fdd26906SClaire Weinan #ifdef BMCWEB_ENABLE_REDFISH_DUMP_LOG 2049fdd26906SClaire Weinan auto respHandler = 2050fdd26906SClaire Weinan [asyncResp]( 2051fdd26906SClaire Weinan const boost::system::error_code ec, 2052fdd26906SClaire Weinan const dbus::utility::MapperGetSubTreePathsResponse& subTreePaths) { 2053fdd26906SClaire Weinan if (ec) 2054fdd26906SClaire Weinan { 2055fdd26906SClaire Weinan BMCWEB_LOG_ERROR 2056dd72e87bSClaire Weinan << "handleBMCLogServicesCollectionGet respHandler got error " 2057fdd26906SClaire Weinan << ec; 2058fdd26906SClaire Weinan // Assume that getting an error simply means there are no dump 2059fdd26906SClaire Weinan // LogServices. Return without adding any error response. 2060fdd26906SClaire Weinan return; 2061fdd26906SClaire Weinan } 2062fdd26906SClaire Weinan 2063fdd26906SClaire Weinan nlohmann::json& logServiceArrayLocal = 2064fdd26906SClaire Weinan asyncResp->res.jsonValue["Members"]; 2065fdd26906SClaire Weinan 2066fdd26906SClaire Weinan for (const std::string& path : subTreePaths) 2067fdd26906SClaire Weinan { 2068fdd26906SClaire Weinan if (path == "/xyz/openbmc_project/dump/bmc") 2069fdd26906SClaire Weinan { 2070fdd26906SClaire Weinan logServiceArrayLocal.push_back( 2071fdd26906SClaire Weinan {{"@odata.id", 2072fdd26906SClaire Weinan "/redfish/v1/Managers/bmc/LogServices/Dump"}}); 2073fdd26906SClaire Weinan } 2074fdd26906SClaire Weinan else if (path == "/xyz/openbmc_project/dump/faultlog") 2075fdd26906SClaire Weinan { 2076fdd26906SClaire Weinan logServiceArrayLocal.push_back( 2077fdd26906SClaire Weinan {{"@odata.id", 2078fdd26906SClaire Weinan "/redfish/v1/Managers/bmc/LogServices/FaultLog"}}); 2079fdd26906SClaire Weinan } 2080fdd26906SClaire Weinan } 2081fdd26906SClaire Weinan 2082e1f26343SJason M. Bills asyncResp->res.jsonValue["Members@odata.count"] = 2083fdd26906SClaire Weinan logServiceArrayLocal.size(); 2084fdd26906SClaire Weinan }; 2085fdd26906SClaire Weinan 2086fdd26906SClaire Weinan crow::connections::systemBus->async_method_call( 2087fdd26906SClaire Weinan respHandler, "xyz.openbmc_project.ObjectMapper", 2088fdd26906SClaire Weinan "/xyz/openbmc_project/object_mapper", 2089fdd26906SClaire Weinan "xyz.openbmc_project.ObjectMapper", "GetSubTreePaths", 2090fdd26906SClaire Weinan "/xyz/openbmc_project/dump", 0, 2091fdd26906SClaire Weinan std::array<const char*, 1>{dumpManagerIface}); 2092fdd26906SClaire Weinan #endif 2093fdd26906SClaire Weinan } 2094fdd26906SClaire Weinan 2095fdd26906SClaire Weinan inline void requestRoutesBMCLogServiceCollection(App& app) 2096fdd26906SClaire Weinan { 2097fdd26906SClaire Weinan BMCWEB_ROUTE(app, "/redfish/v1/Managers/bmc/LogServices/") 2098fdd26906SClaire Weinan .privileges(redfish::privileges::getLogServiceCollection) 2099fdd26906SClaire Weinan .methods(boost::beast::http::verb::get)( 2100dd72e87bSClaire Weinan std::bind_front(handleBMCLogServicesCollectionGet, std::ref(app))); 2101e1f26343SJason M. Bills } 2102e1f26343SJason M. Bills 21037e860f15SJohn Edward Broadbent inline void requestRoutesBMCJournalLogService(App& app) 2104e1f26343SJason M. Bills { 21057e860f15SJohn Edward Broadbent BMCWEB_ROUTE(app, "/redfish/v1/Managers/bmc/LogServices/Journal/") 2106ed398213SEd Tanous .privileges(redfish::privileges::getLogService) 21077e860f15SJohn Edward Broadbent .methods(boost::beast::http::verb::get)( 210845ca1b86SEd Tanous [&app](const crow::Request& req, 210945ca1b86SEd Tanous const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) { 21103ba00073SCarson Labrado if (!redfish::setUpRedfishRoute(app, req, asyncResp)) 21117e860f15SJohn Edward Broadbent { 211245ca1b86SEd Tanous return; 211345ca1b86SEd Tanous } 2114e1f26343SJason M. Bills asyncResp->res.jsonValue["@odata.type"] = 2115e1f26343SJason M. Bills "#LogService.v1_1_0.LogService"; 21160f74e643SEd Tanous asyncResp->res.jsonValue["@odata.id"] = 21170f74e643SEd Tanous "/redfish/v1/Managers/bmc/LogServices/Journal"; 2118002d39b4SEd Tanous asyncResp->res.jsonValue["Name"] = "Open BMC Journal Log Service"; 2119002d39b4SEd Tanous asyncResp->res.jsonValue["Description"] = "BMC Journal Log Service"; 2120c4bf6374SJason M. Bills asyncResp->res.jsonValue["Id"] = "BMC Journal"; 2121e1f26343SJason M. Bills asyncResp->res.jsonValue["OverWritePolicy"] = "WrapsWhenFull"; 21227c8c4058STejas Patil 21237c8c4058STejas Patil std::pair<std::string, std::string> redfishDateTimeOffset = 21242b82937eSEd Tanous redfish::time_utils::getDateTimeOffsetNow(); 2125002d39b4SEd Tanous asyncResp->res.jsonValue["DateTime"] = redfishDateTimeOffset.first; 21267c8c4058STejas Patil asyncResp->res.jsonValue["DateTimeLocalOffset"] = 21277c8c4058STejas Patil redfishDateTimeOffset.second; 21287c8c4058STejas Patil 21291476687dSEd Tanous asyncResp->res.jsonValue["Entries"]["@odata.id"] = 21301476687dSEd Tanous "/redfish/v1/Managers/bmc/LogServices/Journal/Entries"; 21317e860f15SJohn Edward Broadbent }); 2132e1f26343SJason M. Bills } 2133e1f26343SJason M. Bills 21343a48b3a2SJason M. Bills static int 21353a48b3a2SJason M. Bills fillBMCJournalLogEntryJson(const std::string& bmcJournalLogEntryID, 2136e1f26343SJason M. Bills sd_journal* journal, 21373a48b3a2SJason M. Bills nlohmann::json::object_t& bmcJournalLogEntryJson) 2138e1f26343SJason M. Bills { 2139e1f26343SJason M. Bills // Get the Log Entry contents 2140e1f26343SJason M. Bills int ret = 0; 2141e1f26343SJason M. Bills 2142a8fe54f0SJason M. Bills std::string message; 2143a8fe54f0SJason M. Bills std::string_view syslogID; 2144a8fe54f0SJason M. Bills ret = getJournalMetadata(journal, "SYSLOG_IDENTIFIER", syslogID); 2145a8fe54f0SJason M. Bills if (ret < 0) 2146a8fe54f0SJason M. Bills { 2147a8fe54f0SJason M. Bills BMCWEB_LOG_ERROR << "Failed to read SYSLOG_IDENTIFIER field: " 2148a8fe54f0SJason M. Bills << strerror(-ret); 2149a8fe54f0SJason M. Bills } 2150a8fe54f0SJason M. Bills if (!syslogID.empty()) 2151a8fe54f0SJason M. Bills { 2152a8fe54f0SJason M. Bills message += std::string(syslogID) + ": "; 2153a8fe54f0SJason M. Bills } 2154a8fe54f0SJason M. Bills 215539e77504SEd Tanous std::string_view msg; 215616428a1aSJason M. Bills ret = getJournalMetadata(journal, "MESSAGE", msg); 2157e1f26343SJason M. Bills if (ret < 0) 2158e1f26343SJason M. Bills { 2159e1f26343SJason M. Bills BMCWEB_LOG_ERROR << "Failed to read MESSAGE field: " << strerror(-ret); 2160e1f26343SJason M. Bills return 1; 2161e1f26343SJason M. Bills } 2162a8fe54f0SJason M. Bills message += std::string(msg); 2163e1f26343SJason M. Bills 2164e1f26343SJason M. Bills // Get the severity from the PRIORITY field 2165271584abSEd Tanous long int severity = 8; // Default to an invalid priority 216616428a1aSJason M. Bills ret = getJournalMetadata(journal, "PRIORITY", 10, severity); 2167e1f26343SJason M. Bills if (ret < 0) 2168e1f26343SJason M. Bills { 2169e1f26343SJason M. Bills BMCWEB_LOG_ERROR << "Failed to read PRIORITY field: " << strerror(-ret); 2170e1f26343SJason M. Bills } 2171e1f26343SJason M. Bills 2172e1f26343SJason M. Bills // Get the Created time from the timestamp 217316428a1aSJason M. Bills std::string entryTimeStr; 217416428a1aSJason M. Bills if (!getEntryTimestamp(journal, entryTimeStr)) 2175e1f26343SJason M. Bills { 217616428a1aSJason M. Bills return 1; 2177e1f26343SJason M. Bills } 2178e1f26343SJason M. Bills 2179e1f26343SJason M. Bills // Fill in the log entry with the gathered data 218084afc48bSJason M. Bills bmcJournalLogEntryJson["@odata.type"] = "#LogEntry.v1_8_0.LogEntry"; 218184afc48bSJason M. Bills bmcJournalLogEntryJson["@odata.id"] = 218284afc48bSJason M. Bills "/redfish/v1/Managers/bmc/LogServices/Journal/Entries/" + 218384afc48bSJason M. Bills bmcJournalLogEntryID; 218484afc48bSJason M. Bills bmcJournalLogEntryJson["Name"] = "BMC Journal Entry"; 218584afc48bSJason M. Bills bmcJournalLogEntryJson["Id"] = bmcJournalLogEntryID; 218684afc48bSJason M. Bills bmcJournalLogEntryJson["Message"] = std::move(message); 218784afc48bSJason M. Bills bmcJournalLogEntryJson["EntryType"] = "Oem"; 218884afc48bSJason M. Bills bmcJournalLogEntryJson["Severity"] = severity <= 2 ? "Critical" 2189738c1e61SPatrick Williams : severity <= 4 ? "Warning" 219084afc48bSJason M. Bills : "OK"; 219184afc48bSJason M. Bills bmcJournalLogEntryJson["OemRecordFormat"] = "BMC Journal Entry"; 219284afc48bSJason M. Bills bmcJournalLogEntryJson["Created"] = std::move(entryTimeStr); 2193e1f26343SJason M. Bills return 0; 2194e1f26343SJason M. Bills } 2195e1f26343SJason M. Bills 21967e860f15SJohn Edward Broadbent inline void requestRoutesBMCJournalLogEntryCollection(App& app) 2197e1f26343SJason M. Bills { 21987e860f15SJohn Edward Broadbent BMCWEB_ROUTE(app, "/redfish/v1/Managers/bmc/LogServices/Journal/Entries/") 2199ed398213SEd Tanous .privileges(redfish::privileges::getLogEntryCollection) 2200002d39b4SEd Tanous .methods(boost::beast::http::verb::get)( 2201002d39b4SEd Tanous [&app](const crow::Request& req, 2202002d39b4SEd Tanous const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) { 2203c937d2bfSEd Tanous query_param::QueryCapabilities capabilities = { 2204c937d2bfSEd Tanous .canDelegateTop = true, 2205c937d2bfSEd Tanous .canDelegateSkip = true, 2206c937d2bfSEd Tanous }; 2207c937d2bfSEd Tanous query_param::Query delegatedQuery; 2208c937d2bfSEd Tanous if (!redfish::setUpRedfishRouteWithDelegation( 22093ba00073SCarson Labrado app, req, asyncResp, delegatedQuery, capabilities)) 2210193ad2faSJason M. Bills { 2211193ad2faSJason M. Bills return; 2212193ad2faSJason M. Bills } 22133648c8beSEd Tanous 22143648c8beSEd Tanous size_t skip = delegatedQuery.skip.value_or(0); 22155143f7a5SJiaqing Zhao size_t top = delegatedQuery.top.value_or(query_param::Query::maxTop); 22163648c8beSEd Tanous 22177e860f15SJohn Edward Broadbent // Collections don't include the static data added by SubRoute 22187e860f15SJohn Edward Broadbent // because it has a duplicate entry for members 2219e1f26343SJason M. Bills asyncResp->res.jsonValue["@odata.type"] = 2220e1f26343SJason M. Bills "#LogEntryCollection.LogEntryCollection"; 22210f74e643SEd Tanous asyncResp->res.jsonValue["@odata.id"] = 22220f74e643SEd Tanous "/redfish/v1/Managers/bmc/LogServices/Journal/Entries"; 2223e1f26343SJason M. Bills asyncResp->res.jsonValue["Name"] = "Open BMC Journal Entries"; 2224e1f26343SJason M. Bills asyncResp->res.jsonValue["Description"] = 2225e1f26343SJason M. Bills "Collection of BMC Journal Entries"; 22260fda0f12SGeorge Liu nlohmann::json& logEntryArray = asyncResp->res.jsonValue["Members"]; 2227e1f26343SJason M. Bills logEntryArray = nlohmann::json::array(); 2228e1f26343SJason M. Bills 22297e860f15SJohn Edward Broadbent // Go through the journal and use the timestamp to create a 22307e860f15SJohn Edward Broadbent // unique ID for each entry 2231e1f26343SJason M. Bills sd_journal* journalTmp = nullptr; 2232e1f26343SJason M. Bills int ret = sd_journal_open(&journalTmp, SD_JOURNAL_LOCAL_ONLY); 2233e1f26343SJason M. Bills if (ret < 0) 2234e1f26343SJason M. Bills { 2235002d39b4SEd Tanous BMCWEB_LOG_ERROR << "failed to open journal: " << strerror(-ret); 2236f12894f8SJason M. Bills messages::internalError(asyncResp->res); 2237e1f26343SJason M. Bills return; 2238e1f26343SJason M. Bills } 22390fda0f12SGeorge Liu std::unique_ptr<sd_journal, decltype(&sd_journal_close)> journal( 22400fda0f12SGeorge Liu journalTmp, sd_journal_close); 2241e1f26343SJason M. Bills journalTmp = nullptr; 2242b01bf299SEd Tanous uint64_t entryCount = 0; 2243e85d6b16SJason M. Bills // Reset the unique ID on the first entry 2244e85d6b16SJason M. Bills bool firstEntry = true; 2245e1f26343SJason M. Bills SD_JOURNAL_FOREACH(journal.get()) 2246e1f26343SJason M. Bills { 2247193ad2faSJason M. Bills entryCount++; 22487e860f15SJohn Edward Broadbent // Handle paging using skip (number of entries to skip from 22497e860f15SJohn Edward Broadbent // the start) and top (number of entries to display) 22503648c8beSEd Tanous if (entryCount <= skip || entryCount > skip + top) 2251193ad2faSJason M. Bills { 2252193ad2faSJason M. Bills continue; 2253193ad2faSJason M. Bills } 2254193ad2faSJason M. Bills 225516428a1aSJason M. Bills std::string idStr; 2256e85d6b16SJason M. Bills if (!getUniqueEntryID(journal.get(), idStr, firstEntry)) 2257e1f26343SJason M. Bills { 2258e1f26343SJason M. Bills continue; 2259e1f26343SJason M. Bills } 2260e85d6b16SJason M. Bills firstEntry = false; 2261e85d6b16SJason M. Bills 22623a48b3a2SJason M. Bills nlohmann::json::object_t bmcJournalLogEntry; 2263c4bf6374SJason M. Bills if (fillBMCJournalLogEntryJson(idStr, journal.get(), 2264c4bf6374SJason M. Bills bmcJournalLogEntry) != 0) 2265e1f26343SJason M. Bills { 2266f12894f8SJason M. Bills messages::internalError(asyncResp->res); 2267e1f26343SJason M. Bills return; 2268e1f26343SJason M. Bills } 22693a48b3a2SJason M. Bills logEntryArray.push_back(std::move(bmcJournalLogEntry)); 2270e1f26343SJason M. Bills } 2271193ad2faSJason M. Bills asyncResp->res.jsonValue["Members@odata.count"] = entryCount; 22723648c8beSEd Tanous if (skip + top < entryCount) 2273193ad2faSJason M. Bills { 2274193ad2faSJason M. Bills asyncResp->res.jsonValue["Members@odata.nextLink"] = 22750fda0f12SGeorge Liu "/redfish/v1/Managers/bmc/LogServices/Journal/Entries?$skip=" + 22763648c8beSEd Tanous std::to_string(skip + top); 2277193ad2faSJason M. Bills } 22787e860f15SJohn Edward Broadbent }); 2279e1f26343SJason M. Bills } 2280e1f26343SJason M. Bills 22817e860f15SJohn Edward Broadbent inline void requestRoutesBMCJournalLogEntry(App& app) 2282e1f26343SJason M. Bills { 22837e860f15SJohn Edward Broadbent BMCWEB_ROUTE(app, 22847e860f15SJohn Edward Broadbent "/redfish/v1/Managers/bmc/LogServices/Journal/Entries/<str>/") 2285ed398213SEd Tanous .privileges(redfish::privileges::getLogEntry) 22867e860f15SJohn Edward Broadbent .methods(boost::beast::http::verb::get)( 228745ca1b86SEd Tanous [&app](const crow::Request& req, 22887e860f15SJohn Edward Broadbent const std::shared_ptr<bmcweb::AsyncResp>& asyncResp, 22897e860f15SJohn Edward Broadbent const std::string& entryID) { 22903ba00073SCarson Labrado if (!redfish::setUpRedfishRoute(app, req, asyncResp)) 229145ca1b86SEd Tanous { 229245ca1b86SEd Tanous return; 229345ca1b86SEd Tanous } 2294e1f26343SJason M. Bills // Convert the unique ID back to a timestamp to find the entry 2295e1f26343SJason M. Bills uint64_t ts = 0; 2296271584abSEd Tanous uint64_t index = 0; 22978d1b46d7Szhanghch05 if (!getTimestampFromID(asyncResp, entryID, ts, index)) 2298e1f26343SJason M. Bills { 229916428a1aSJason M. Bills return; 2300e1f26343SJason M. Bills } 2301e1f26343SJason M. Bills 2302e1f26343SJason M. Bills sd_journal* journalTmp = nullptr; 2303e1f26343SJason M. Bills int ret = sd_journal_open(&journalTmp, SD_JOURNAL_LOCAL_ONLY); 2304e1f26343SJason M. Bills if (ret < 0) 2305e1f26343SJason M. Bills { 2306002d39b4SEd Tanous BMCWEB_LOG_ERROR << "failed to open journal: " << strerror(-ret); 2307f12894f8SJason M. Bills messages::internalError(asyncResp->res); 2308e1f26343SJason M. Bills return; 2309e1f26343SJason M. Bills } 2310002d39b4SEd Tanous std::unique_ptr<sd_journal, decltype(&sd_journal_close)> journal( 2311002d39b4SEd Tanous journalTmp, sd_journal_close); 2312e1f26343SJason M. Bills journalTmp = nullptr; 23137e860f15SJohn Edward Broadbent // Go to the timestamp in the log and move to the entry at the 23147e860f15SJohn Edward Broadbent // index tracking the unique ID 2315af07e3f5SJason M. Bills std::string idStr; 2316af07e3f5SJason M. Bills bool firstEntry = true; 2317e1f26343SJason M. Bills ret = sd_journal_seek_realtime_usec(journal.get(), ts); 23182056b6d1SManojkiran Eda if (ret < 0) 23192056b6d1SManojkiran Eda { 23202056b6d1SManojkiran Eda BMCWEB_LOG_ERROR << "failed to seek to an entry in journal" 23212056b6d1SManojkiran Eda << strerror(-ret); 23222056b6d1SManojkiran Eda messages::internalError(asyncResp->res); 23232056b6d1SManojkiran Eda return; 23242056b6d1SManojkiran Eda } 2325271584abSEd Tanous for (uint64_t i = 0; i <= index; i++) 2326e1f26343SJason M. Bills { 2327e1f26343SJason M. Bills sd_journal_next(journal.get()); 2328af07e3f5SJason M. Bills if (!getUniqueEntryID(journal.get(), idStr, firstEntry)) 2329af07e3f5SJason M. Bills { 2330af07e3f5SJason M. Bills messages::internalError(asyncResp->res); 2331af07e3f5SJason M. Bills return; 2332af07e3f5SJason M. Bills } 2333af07e3f5SJason M. Bills firstEntry = false; 2334af07e3f5SJason M. Bills } 2335c4bf6374SJason M. Bills // Confirm that the entry ID matches what was requested 2336af07e3f5SJason M. Bills if (idStr != entryID) 2337c4bf6374SJason M. Bills { 2338ace85d60SEd Tanous messages::resourceMissingAtURI(asyncResp->res, req.urlView); 2339c4bf6374SJason M. Bills return; 2340c4bf6374SJason M. Bills } 2341c4bf6374SJason M. Bills 23423a48b3a2SJason M. Bills nlohmann::json::object_t bmcJournalLogEntry; 2343c4bf6374SJason M. Bills if (fillBMCJournalLogEntryJson(entryID, journal.get(), 23443a48b3a2SJason M. Bills bmcJournalLogEntry) != 0) 2345e1f26343SJason M. Bills { 2346f12894f8SJason M. Bills messages::internalError(asyncResp->res); 2347e1f26343SJason M. Bills return; 2348e1f26343SJason M. Bills } 2349d405bb51SJason M. Bills asyncResp->res.jsonValue.update(bmcJournalLogEntry); 23507e860f15SJohn Edward Broadbent }); 2351c9bb6861Sraviteja-b } 2352c9bb6861Sraviteja-b 2353fdd26906SClaire Weinan inline void 2354fdd26906SClaire Weinan getDumpServiceInfo(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp, 2355fdd26906SClaire Weinan const std::string& dumpType) 2356c9bb6861Sraviteja-b { 2357fdd26906SClaire Weinan std::string dumpPath; 2358fdd26906SClaire Weinan std::string overWritePolicy; 2359fdd26906SClaire Weinan bool collectDiagnosticDataSupported = false; 2360fdd26906SClaire Weinan 2361fdd26906SClaire Weinan if (dumpType == "BMC") 236245ca1b86SEd Tanous { 2363fdd26906SClaire Weinan dumpPath = "/redfish/v1/Managers/bmc/LogServices/Dump"; 2364fdd26906SClaire Weinan overWritePolicy = "WrapsWhenFull"; 2365fdd26906SClaire Weinan collectDiagnosticDataSupported = true; 2366fdd26906SClaire Weinan } 2367fdd26906SClaire Weinan else if (dumpType == "FaultLog") 2368fdd26906SClaire Weinan { 2369fdd26906SClaire Weinan dumpPath = "/redfish/v1/Managers/bmc/LogServices/FaultLog"; 2370fdd26906SClaire Weinan overWritePolicy = "Unknown"; 2371fdd26906SClaire Weinan collectDiagnosticDataSupported = false; 2372fdd26906SClaire Weinan } 2373fdd26906SClaire Weinan else if (dumpType == "System") 2374fdd26906SClaire Weinan { 2375fdd26906SClaire Weinan dumpPath = "/redfish/v1/Systems/system/LogServices/Dump"; 2376fdd26906SClaire Weinan overWritePolicy = "WrapsWhenFull"; 2377fdd26906SClaire Weinan collectDiagnosticDataSupported = true; 2378fdd26906SClaire Weinan } 2379fdd26906SClaire Weinan else 2380fdd26906SClaire Weinan { 2381fdd26906SClaire Weinan BMCWEB_LOG_ERROR << "getDumpServiceInfo() invalid dump type: " 2382fdd26906SClaire Weinan << dumpType; 2383fdd26906SClaire Weinan messages::internalError(asyncResp->res); 238445ca1b86SEd Tanous return; 238545ca1b86SEd Tanous } 2386fdd26906SClaire Weinan 2387fdd26906SClaire Weinan asyncResp->res.jsonValue["@odata.id"] = dumpPath; 2388fdd26906SClaire Weinan asyncResp->res.jsonValue["@odata.type"] = "#LogService.v1_2_0.LogService"; 2389c9bb6861Sraviteja-b asyncResp->res.jsonValue["Name"] = "Dump LogService"; 2390fdd26906SClaire Weinan asyncResp->res.jsonValue["Description"] = dumpType + " Dump LogService"; 2391fdd26906SClaire Weinan asyncResp->res.jsonValue["Id"] = std::filesystem::path(dumpPath).filename(); 2392fdd26906SClaire Weinan asyncResp->res.jsonValue["OverWritePolicy"] = std::move(overWritePolicy); 23937c8c4058STejas Patil 23947c8c4058STejas Patil std::pair<std::string, std::string> redfishDateTimeOffset = 23952b82937eSEd Tanous redfish::time_utils::getDateTimeOffsetNow(); 23960fda0f12SGeorge Liu asyncResp->res.jsonValue["DateTime"] = redfishDateTimeOffset.first; 23977c8c4058STejas Patil asyncResp->res.jsonValue["DateTimeLocalOffset"] = 23987c8c4058STejas Patil redfishDateTimeOffset.second; 23997c8c4058STejas Patil 2400fdd26906SClaire Weinan asyncResp->res.jsonValue["Entries"]["@odata.id"] = dumpPath + "/Entries"; 2401002d39b4SEd Tanous asyncResp->res.jsonValue["Actions"]["#LogService.ClearLog"]["target"] = 2402fdd26906SClaire Weinan dumpPath + "/Actions/LogService.ClearLog"; 2403fdd26906SClaire Weinan 2404fdd26906SClaire Weinan if (collectDiagnosticDataSupported) 2405fdd26906SClaire Weinan { 2406002d39b4SEd Tanous asyncResp->res.jsonValue["Actions"]["#LogService.CollectDiagnosticData"] 24071476687dSEd Tanous ["target"] = 2408fdd26906SClaire Weinan dumpPath + "/Actions/LogService.CollectDiagnosticData"; 2409fdd26906SClaire Weinan } 2410c9bb6861Sraviteja-b } 2411c9bb6861Sraviteja-b 2412fdd26906SClaire Weinan inline void handleLogServicesDumpServiceGet( 2413fdd26906SClaire Weinan crow::App& app, const std::string& dumpType, const crow::Request& req, 2414fdd26906SClaire Weinan const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) 24157e860f15SJohn Edward Broadbent { 24163ba00073SCarson Labrado if (!redfish::setUpRedfishRoute(app, req, asyncResp)) 241745ca1b86SEd Tanous { 241845ca1b86SEd Tanous return; 241945ca1b86SEd Tanous } 2420fdd26906SClaire Weinan getDumpServiceInfo(asyncResp, dumpType); 2421fdd26906SClaire Weinan } 2422c9bb6861Sraviteja-b 2423fdd26906SClaire Weinan inline void handleLogServicesDumpEntriesCollectionGet( 2424fdd26906SClaire Weinan crow::App& app, const std::string& dumpType, const crow::Request& req, 2425fdd26906SClaire Weinan const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) 2426fdd26906SClaire Weinan { 2427fdd26906SClaire Weinan if (!redfish::setUpRedfishRoute(app, req, asyncResp)) 2428fdd26906SClaire Weinan { 2429fdd26906SClaire Weinan return; 2430fdd26906SClaire Weinan } 2431fdd26906SClaire Weinan getDumpEntryCollection(asyncResp, dumpType); 2432fdd26906SClaire Weinan } 2433fdd26906SClaire Weinan 2434fdd26906SClaire Weinan inline void handleLogServicesDumpEntryGet( 2435fdd26906SClaire Weinan crow::App& app, const std::string& dumpType, const crow::Request& req, 2436fdd26906SClaire Weinan const std::shared_ptr<bmcweb::AsyncResp>& asyncResp, 2437fdd26906SClaire Weinan const std::string& dumpId) 2438fdd26906SClaire Weinan { 2439fdd26906SClaire Weinan if (!redfish::setUpRedfishRoute(app, req, asyncResp)) 2440fdd26906SClaire Weinan { 2441fdd26906SClaire Weinan return; 2442fdd26906SClaire Weinan } 2443fdd26906SClaire Weinan getDumpEntryById(asyncResp, dumpId, dumpType); 2444fdd26906SClaire Weinan } 2445fdd26906SClaire Weinan 2446fdd26906SClaire Weinan inline void handleLogServicesDumpEntryDelete( 2447fdd26906SClaire Weinan crow::App& app, const std::string& dumpType, const crow::Request& req, 2448fdd26906SClaire Weinan const std::shared_ptr<bmcweb::AsyncResp>& asyncResp, 2449fdd26906SClaire Weinan const std::string& dumpId) 2450fdd26906SClaire Weinan { 2451fdd26906SClaire Weinan if (!redfish::setUpRedfishRoute(app, req, asyncResp)) 2452fdd26906SClaire Weinan { 2453fdd26906SClaire Weinan return; 2454fdd26906SClaire Weinan } 2455fdd26906SClaire Weinan deleteDumpEntry(asyncResp, dumpId, dumpType); 2456fdd26906SClaire Weinan } 2457fdd26906SClaire Weinan 2458fdd26906SClaire Weinan inline void handleLogServicesDumpCollectDiagnosticDataPost( 2459fdd26906SClaire Weinan crow::App& app, const std::string& dumpType, const crow::Request& req, 2460fdd26906SClaire Weinan const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) 2461fdd26906SClaire Weinan { 2462fdd26906SClaire Weinan if (!redfish::setUpRedfishRoute(app, req, asyncResp)) 2463fdd26906SClaire Weinan { 2464fdd26906SClaire Weinan return; 2465fdd26906SClaire Weinan } 2466fdd26906SClaire Weinan createDump(asyncResp, req, dumpType); 2467fdd26906SClaire Weinan } 2468fdd26906SClaire Weinan 2469fdd26906SClaire Weinan inline void handleLogServicesDumpClearLogPost( 2470fdd26906SClaire Weinan crow::App& app, const std::string& dumpType, const crow::Request& req, 2471fdd26906SClaire Weinan const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) 2472fdd26906SClaire Weinan { 2473fdd26906SClaire Weinan if (!redfish::setUpRedfishRoute(app, req, asyncResp)) 2474fdd26906SClaire Weinan { 2475fdd26906SClaire Weinan return; 2476fdd26906SClaire Weinan } 2477fdd26906SClaire Weinan clearDump(asyncResp, dumpType); 2478fdd26906SClaire Weinan } 2479fdd26906SClaire Weinan 2480fdd26906SClaire Weinan inline void requestRoutesBMCDumpService(App& app) 2481fdd26906SClaire Weinan { 2482fdd26906SClaire Weinan BMCWEB_ROUTE(app, "/redfish/v1/Managers/bmc/LogServices/Dump/") 2483fdd26906SClaire Weinan .privileges(redfish::privileges::getLogService) 2484fdd26906SClaire Weinan .methods(boost::beast::http::verb::get)(std::bind_front( 2485fdd26906SClaire Weinan handleLogServicesDumpServiceGet, std::ref(app), "BMC")); 2486fdd26906SClaire Weinan } 2487fdd26906SClaire Weinan 2488fdd26906SClaire Weinan inline void requestRoutesBMCDumpEntryCollection(App& app) 2489fdd26906SClaire Weinan { 2490fdd26906SClaire Weinan BMCWEB_ROUTE(app, "/redfish/v1/Managers/bmc/LogServices/Dump/Entries/") 2491fdd26906SClaire Weinan .privileges(redfish::privileges::getLogEntryCollection) 2492fdd26906SClaire Weinan .methods(boost::beast::http::verb::get)(std::bind_front( 2493fdd26906SClaire Weinan handleLogServicesDumpEntriesCollectionGet, std::ref(app), "BMC")); 2494c9bb6861Sraviteja-b } 2495c9bb6861Sraviteja-b 24967e860f15SJohn Edward Broadbent inline void requestRoutesBMCDumpEntry(App& app) 2497c9bb6861Sraviteja-b { 24987e860f15SJohn Edward Broadbent BMCWEB_ROUTE(app, 24997e860f15SJohn Edward Broadbent "/redfish/v1/Managers/bmc/LogServices/Dump/Entries/<str>/") 2500ed398213SEd Tanous .privileges(redfish::privileges::getLogEntry) 2501fdd26906SClaire Weinan .methods(boost::beast::http::verb::get)(std::bind_front( 2502fdd26906SClaire Weinan handleLogServicesDumpEntryGet, std::ref(app), "BMC")); 2503fdd26906SClaire Weinan 25047e860f15SJohn Edward Broadbent BMCWEB_ROUTE(app, 25057e860f15SJohn Edward Broadbent "/redfish/v1/Managers/bmc/LogServices/Dump/Entries/<str>/") 2506ed398213SEd Tanous .privileges(redfish::privileges::deleteLogEntry) 2507fdd26906SClaire Weinan .methods(boost::beast::http::verb::delete_)(std::bind_front( 2508fdd26906SClaire Weinan handleLogServicesDumpEntryDelete, std::ref(app), "BMC")); 2509c9bb6861Sraviteja-b } 2510c9bb6861Sraviteja-b 25117e860f15SJohn Edward Broadbent inline void requestRoutesBMCDumpCreate(App& app) 2512c9bb6861Sraviteja-b { 25130fda0f12SGeorge Liu BMCWEB_ROUTE( 25140fda0f12SGeorge Liu app, 25150fda0f12SGeorge Liu "/redfish/v1/Managers/bmc/LogServices/Dump/Actions/LogService.CollectDiagnosticData/") 2516ed398213SEd Tanous .privileges(redfish::privileges::postLogService) 25177e860f15SJohn Edward Broadbent .methods(boost::beast::http::verb::post)( 2518fdd26906SClaire Weinan std::bind_front(handleLogServicesDumpCollectDiagnosticDataPost, 2519fdd26906SClaire Weinan std::ref(app), "BMC")); 2520a43be80fSAsmitha Karunanithi } 2521a43be80fSAsmitha Karunanithi 25227e860f15SJohn Edward Broadbent inline void requestRoutesBMCDumpClear(App& app) 252380319af1SAsmitha Karunanithi { 25240fda0f12SGeorge Liu BMCWEB_ROUTE( 25250fda0f12SGeorge Liu app, 25260fda0f12SGeorge Liu "/redfish/v1/Managers/bmc/LogServices/Dump/Actions/LogService.ClearLog/") 2527ed398213SEd Tanous .privileges(redfish::privileges::postLogService) 2528fdd26906SClaire Weinan .methods(boost::beast::http::verb::post)(std::bind_front( 2529fdd26906SClaire Weinan handleLogServicesDumpClearLogPost, std::ref(app), "BMC")); 253045ca1b86SEd Tanous } 2531fdd26906SClaire Weinan 2532fdd26906SClaire Weinan inline void requestRoutesFaultLogDumpService(App& app) 2533fdd26906SClaire Weinan { 2534fdd26906SClaire Weinan BMCWEB_ROUTE(app, "/redfish/v1/Managers/bmc/LogServices/FaultLog/") 2535fdd26906SClaire Weinan .privileges(redfish::privileges::getLogService) 2536fdd26906SClaire Weinan .methods(boost::beast::http::verb::get)(std::bind_front( 2537fdd26906SClaire Weinan handleLogServicesDumpServiceGet, std::ref(app), "FaultLog")); 2538fdd26906SClaire Weinan } 2539fdd26906SClaire Weinan 2540fdd26906SClaire Weinan inline void requestRoutesFaultLogDumpEntryCollection(App& app) 2541fdd26906SClaire Weinan { 2542fdd26906SClaire Weinan BMCWEB_ROUTE(app, "/redfish/v1/Managers/bmc/LogServices/FaultLog/Entries/") 2543fdd26906SClaire Weinan .privileges(redfish::privileges::getLogEntryCollection) 2544fdd26906SClaire Weinan .methods(boost::beast::http::verb::get)( 2545fdd26906SClaire Weinan std::bind_front(handleLogServicesDumpEntriesCollectionGet, 2546fdd26906SClaire Weinan std::ref(app), "FaultLog")); 2547fdd26906SClaire Weinan } 2548fdd26906SClaire Weinan 2549fdd26906SClaire Weinan inline void requestRoutesFaultLogDumpEntry(App& app) 2550fdd26906SClaire Weinan { 2551fdd26906SClaire Weinan BMCWEB_ROUTE(app, 2552fdd26906SClaire Weinan "/redfish/v1/Managers/bmc/LogServices/FaultLog/Entries/<str>/") 2553fdd26906SClaire Weinan .privileges(redfish::privileges::getLogEntry) 2554fdd26906SClaire Weinan .methods(boost::beast::http::verb::get)(std::bind_front( 2555fdd26906SClaire Weinan handleLogServicesDumpEntryGet, std::ref(app), "FaultLog")); 2556fdd26906SClaire Weinan 2557fdd26906SClaire Weinan BMCWEB_ROUTE(app, 2558fdd26906SClaire Weinan "/redfish/v1/Managers/bmc/LogServices/FaultLog/Entries/<str>/") 2559fdd26906SClaire Weinan .privileges(redfish::privileges::deleteLogEntry) 2560fdd26906SClaire Weinan .methods(boost::beast::http::verb::delete_)(std::bind_front( 2561fdd26906SClaire Weinan handleLogServicesDumpEntryDelete, std::ref(app), "FaultLog")); 2562fdd26906SClaire Weinan } 2563fdd26906SClaire Weinan 2564fdd26906SClaire Weinan inline void requestRoutesFaultLogDumpClear(App& app) 2565fdd26906SClaire Weinan { 2566fdd26906SClaire Weinan BMCWEB_ROUTE( 2567fdd26906SClaire Weinan app, 2568fdd26906SClaire Weinan "/redfish/v1/Managers/bmc/LogServices/FaultLog/Actions/LogService.ClearLog/") 2569fdd26906SClaire Weinan .privileges(redfish::privileges::postLogService) 2570fdd26906SClaire Weinan .methods(boost::beast::http::verb::post)(std::bind_front( 2571fdd26906SClaire Weinan handleLogServicesDumpClearLogPost, std::ref(app), "FaultLog")); 25725cb1dd27SAsmitha Karunanithi } 25735cb1dd27SAsmitha Karunanithi 25747e860f15SJohn Edward Broadbent inline void requestRoutesSystemDumpService(App& app) 25755cb1dd27SAsmitha Karunanithi { 25767e860f15SJohn Edward Broadbent BMCWEB_ROUTE(app, "/redfish/v1/Systems/system/LogServices/Dump/") 2577ed398213SEd Tanous .privileges(redfish::privileges::getLogService) 2578002d39b4SEd Tanous .methods(boost::beast::http::verb::get)( 2579002d39b4SEd Tanous [&app](const crow::Request& req, 2580002d39b4SEd Tanous const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) { 25813ba00073SCarson Labrado if (!redfish::setUpRedfishRoute(app, req, asyncResp)) 25827e860f15SJohn Edward Broadbent { 258345ca1b86SEd Tanous return; 258445ca1b86SEd Tanous } 25855cb1dd27SAsmitha Karunanithi asyncResp->res.jsonValue["@odata.id"] = 25865cb1dd27SAsmitha Karunanithi "/redfish/v1/Systems/system/LogServices/Dump"; 25875cb1dd27SAsmitha Karunanithi asyncResp->res.jsonValue["@odata.type"] = 2588d337bb72SAsmitha Karunanithi "#LogService.v1_2_0.LogService"; 25895cb1dd27SAsmitha Karunanithi asyncResp->res.jsonValue["Name"] = "Dump LogService"; 259045ca1b86SEd Tanous asyncResp->res.jsonValue["Description"] = "System Dump LogService"; 25915cb1dd27SAsmitha Karunanithi asyncResp->res.jsonValue["Id"] = "Dump"; 25925cb1dd27SAsmitha Karunanithi asyncResp->res.jsonValue["OverWritePolicy"] = "WrapsWhenFull"; 25937c8c4058STejas Patil 25947c8c4058STejas Patil std::pair<std::string, std::string> redfishDateTimeOffset = 25952b82937eSEd Tanous redfish::time_utils::getDateTimeOffsetNow(); 259645ca1b86SEd Tanous asyncResp->res.jsonValue["DateTime"] = redfishDateTimeOffset.first; 25977c8c4058STejas Patil asyncResp->res.jsonValue["DateTimeLocalOffset"] = 25987c8c4058STejas Patil redfishDateTimeOffset.second; 25997c8c4058STejas Patil 26001476687dSEd Tanous asyncResp->res.jsonValue["Entries"]["@odata.id"] = 26011476687dSEd Tanous "/redfish/v1/Systems/system/LogServices/Dump/Entries"; 2602002d39b4SEd Tanous asyncResp->res.jsonValue["Actions"]["#LogService.ClearLog"]["target"] = 26031476687dSEd Tanous "/redfish/v1/Systems/system/LogServices/Dump/Actions/LogService.ClearLog"; 26041476687dSEd Tanous 2605002d39b4SEd Tanous asyncResp->res.jsonValue["Actions"]["#LogService.CollectDiagnosticData"] 26061476687dSEd Tanous ["target"] = 26071476687dSEd Tanous "/redfish/v1/Systems/system/LogServices/Dump/Actions/LogService.CollectDiagnosticData"; 26087e860f15SJohn Edward Broadbent }); 26095cb1dd27SAsmitha Karunanithi } 26105cb1dd27SAsmitha Karunanithi 26117e860f15SJohn Edward Broadbent inline void requestRoutesSystemDumpEntryCollection(App& app) 26127e860f15SJohn Edward Broadbent { 26137e860f15SJohn Edward Broadbent 26145cb1dd27SAsmitha Karunanithi /** 26155cb1dd27SAsmitha Karunanithi * Functions triggers appropriate requests on DBus 26165cb1dd27SAsmitha Karunanithi */ 2617b2a3289dSAsmitha Karunanithi BMCWEB_ROUTE(app, "/redfish/v1/Systems/system/LogServices/Dump/Entries/") 2618ed398213SEd Tanous .privileges(redfish::privileges::getLogEntryCollection) 26197e860f15SJohn Edward Broadbent .methods(boost::beast::http::verb::get)( 262045ca1b86SEd Tanous [&app](const crow::Request& req, 2621864d6a17SJohn Edward Broadbent const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) { 26223ba00073SCarson Labrado if (!redfish::setUpRedfishRoute(app, req, asyncResp)) 262345ca1b86SEd Tanous { 262445ca1b86SEd Tanous return; 262545ca1b86SEd Tanous } 26265cb1dd27SAsmitha Karunanithi getDumpEntryCollection(asyncResp, "System"); 26277e860f15SJohn Edward Broadbent }); 26285cb1dd27SAsmitha Karunanithi } 26295cb1dd27SAsmitha Karunanithi 26307e860f15SJohn Edward Broadbent inline void requestRoutesSystemDumpEntry(App& app) 26315cb1dd27SAsmitha Karunanithi { 26327e860f15SJohn Edward Broadbent BMCWEB_ROUTE(app, 2633864d6a17SJohn Edward Broadbent "/redfish/v1/Systems/system/LogServices/Dump/Entries/<str>/") 2634ed398213SEd Tanous .privileges(redfish::privileges::getLogEntry) 2635ed398213SEd Tanous 26367e860f15SJohn Edward Broadbent .methods(boost::beast::http::verb::get)( 263745ca1b86SEd Tanous [&app](const crow::Request& req, 26387e860f15SJohn Edward Broadbent const std::shared_ptr<bmcweb::AsyncResp>& asyncResp, 26397e860f15SJohn Edward Broadbent const std::string& param) { 26403ba00073SCarson Labrado if (!redfish::setUpRedfishRoute(app, req, asyncResp)) 2641c7a6d660SClaire Weinan { 2642c7a6d660SClaire Weinan return; 2643c7a6d660SClaire Weinan } 2644c7a6d660SClaire Weinan getDumpEntryById(asyncResp, param, "System"); 26457e860f15SJohn Edward Broadbent }); 26468d1b46d7Szhanghch05 26477e860f15SJohn Edward Broadbent BMCWEB_ROUTE(app, 2648864d6a17SJohn Edward Broadbent "/redfish/v1/Systems/system/LogServices/Dump/Entries/<str>/") 2649ed398213SEd Tanous .privileges(redfish::privileges::deleteLogEntry) 26507e860f15SJohn Edward Broadbent .methods(boost::beast::http::verb::delete_)( 265145ca1b86SEd Tanous [&app](const crow::Request& req, 26527e860f15SJohn Edward Broadbent const std::shared_ptr<bmcweb::AsyncResp>& asyncResp, 26537e860f15SJohn Edward Broadbent const std::string& param) { 26543ba00073SCarson Labrado if (!redfish::setUpRedfishRoute(app, req, asyncResp)) 265545ca1b86SEd Tanous { 265645ca1b86SEd Tanous return; 265745ca1b86SEd Tanous } 26587e860f15SJohn Edward Broadbent deleteDumpEntry(asyncResp, param, "system"); 26597e860f15SJohn Edward Broadbent }); 26605cb1dd27SAsmitha Karunanithi } 2661c9bb6861Sraviteja-b 26627e860f15SJohn Edward Broadbent inline void requestRoutesSystemDumpCreate(App& app) 2663c9bb6861Sraviteja-b { 26640fda0f12SGeorge Liu BMCWEB_ROUTE( 26650fda0f12SGeorge Liu app, 26660fda0f12SGeorge Liu "/redfish/v1/Systems/system/LogServices/Dump/Actions/LogService.CollectDiagnosticData/") 2667ed398213SEd Tanous .privileges(redfish::privileges::postLogService) 26687e860f15SJohn Edward Broadbent .methods(boost::beast::http::verb::post)( 266945ca1b86SEd Tanous [&app](const crow::Request& req, 267045ca1b86SEd Tanous const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) { 26713ba00073SCarson Labrado if (!redfish::setUpRedfishRoute(app, req, asyncResp)) 267245ca1b86SEd Tanous { 267345ca1b86SEd Tanous return; 267445ca1b86SEd Tanous } 267545ca1b86SEd Tanous createDump(asyncResp, req, "System"); 267645ca1b86SEd Tanous }); 2677a43be80fSAsmitha Karunanithi } 2678a43be80fSAsmitha Karunanithi 26797e860f15SJohn Edward Broadbent inline void requestRoutesSystemDumpClear(App& app) 2680a43be80fSAsmitha Karunanithi { 26810fda0f12SGeorge Liu BMCWEB_ROUTE( 26820fda0f12SGeorge Liu app, 26830fda0f12SGeorge Liu "/redfish/v1/Systems/system/LogServices/Dump/Actions/LogService.ClearLog/") 2684ed398213SEd Tanous .privileges(redfish::privileges::postLogService) 26857e860f15SJohn Edward Broadbent .methods(boost::beast::http::verb::post)( 268645ca1b86SEd Tanous [&app](const crow::Request& req, 26877e860f15SJohn Edward Broadbent const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) 26887e860f15SJohn Edward Broadbent 268945ca1b86SEd Tanous { 26903ba00073SCarson Labrado if (!redfish::setUpRedfishRoute(app, req, asyncResp)) 269145ca1b86SEd Tanous { 269245ca1b86SEd Tanous return; 269345ca1b86SEd Tanous } 269445ca1b86SEd Tanous clearDump(asyncResp, "System"); 269545ca1b86SEd Tanous }); 2696013487e5Sraviteja-b } 2697013487e5Sraviteja-b 26987e860f15SJohn Edward Broadbent inline void requestRoutesCrashdumpService(App& app) 26991da66f75SEd Tanous { 27003946028dSAppaRao Puli // Note: Deviated from redfish privilege registry for GET & HEAD 27013946028dSAppaRao Puli // method for security reasons. 27021da66f75SEd Tanous /** 27031da66f75SEd Tanous * Functions triggers appropriate requests on DBus 27041da66f75SEd Tanous */ 27057e860f15SJohn Edward Broadbent BMCWEB_ROUTE(app, "/redfish/v1/Systems/system/LogServices/Crashdump/") 2706ed398213SEd Tanous // This is incorrect, should be: 2707ed398213SEd Tanous //.privileges(redfish::privileges::getLogService) 2708432a890cSEd Tanous .privileges({{"ConfigureManager"}}) 2709002d39b4SEd Tanous .methods(boost::beast::http::verb::get)( 2710002d39b4SEd Tanous [&app](const crow::Request& req, 2711002d39b4SEd Tanous const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) { 27123ba00073SCarson Labrado if (!redfish::setUpRedfishRoute(app, req, asyncResp)) 271345ca1b86SEd Tanous { 271445ca1b86SEd Tanous return; 271545ca1b86SEd Tanous } 27167e860f15SJohn Edward Broadbent // Copy over the static data to include the entries added by 27177e860f15SJohn Edward Broadbent // SubRoute 27180f74e643SEd Tanous asyncResp->res.jsonValue["@odata.id"] = 2719424c4176SJason M. Bills "/redfish/v1/Systems/system/LogServices/Crashdump"; 2720e1f26343SJason M. Bills asyncResp->res.jsonValue["@odata.type"] = 27218e6c099aSJason M. Bills "#LogService.v1_2_0.LogService"; 27224f50ae4bSGunnar Mills asyncResp->res.jsonValue["Name"] = "Open BMC Oem Crashdump Service"; 27234f50ae4bSGunnar Mills asyncResp->res.jsonValue["Description"] = "Oem Crashdump Service"; 27244f50ae4bSGunnar Mills asyncResp->res.jsonValue["Id"] = "Oem Crashdump"; 2725e1f26343SJason M. Bills asyncResp->res.jsonValue["OverWritePolicy"] = "WrapsWhenFull"; 2726e1f26343SJason M. Bills asyncResp->res.jsonValue["MaxNumberOfRecords"] = 3; 27277c8c4058STejas Patil 27287c8c4058STejas Patil std::pair<std::string, std::string> redfishDateTimeOffset = 27292b82937eSEd Tanous redfish::time_utils::getDateTimeOffsetNow(); 27307c8c4058STejas Patil asyncResp->res.jsonValue["DateTime"] = redfishDateTimeOffset.first; 27317c8c4058STejas Patil asyncResp->res.jsonValue["DateTimeLocalOffset"] = 27327c8c4058STejas Patil redfishDateTimeOffset.second; 27337c8c4058STejas Patil 27341476687dSEd Tanous asyncResp->res.jsonValue["Entries"]["@odata.id"] = 27351476687dSEd Tanous "/redfish/v1/Systems/system/LogServices/Crashdump/Entries"; 2736002d39b4SEd Tanous asyncResp->res.jsonValue["Actions"]["#LogService.ClearLog"]["target"] = 27371476687dSEd Tanous "/redfish/v1/Systems/system/LogServices/Crashdump/Actions/LogService.ClearLog"; 2738002d39b4SEd Tanous asyncResp->res.jsonValue["Actions"]["#LogService.CollectDiagnosticData"] 27391476687dSEd Tanous ["target"] = 27401476687dSEd Tanous "/redfish/v1/Systems/system/LogServices/Crashdump/Actions/LogService.CollectDiagnosticData"; 27417e860f15SJohn Edward Broadbent }); 27421da66f75SEd Tanous } 27431da66f75SEd Tanous 27447e860f15SJohn Edward Broadbent void inline requestRoutesCrashdumpClear(App& app) 27455b61b5e8SJason M. Bills { 27460fda0f12SGeorge Liu BMCWEB_ROUTE( 27470fda0f12SGeorge Liu app, 27480fda0f12SGeorge Liu "/redfish/v1/Systems/system/LogServices/Crashdump/Actions/LogService.ClearLog/") 2749ed398213SEd Tanous // This is incorrect, should be: 2750ed398213SEd Tanous //.privileges(redfish::privileges::postLogService) 2751432a890cSEd Tanous .privileges({{"ConfigureComponents"}}) 27527e860f15SJohn Edward Broadbent .methods(boost::beast::http::verb::post)( 275345ca1b86SEd Tanous [&app](const crow::Request& req, 27547e860f15SJohn Edward Broadbent const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) { 27553ba00073SCarson Labrado if (!redfish::setUpRedfishRoute(app, req, asyncResp)) 275645ca1b86SEd Tanous { 275745ca1b86SEd Tanous return; 275845ca1b86SEd Tanous } 27595b61b5e8SJason M. Bills crow::connections::systemBus->async_method_call( 27605b61b5e8SJason M. Bills [asyncResp](const boost::system::error_code ec, 2761cb13a392SEd Tanous const std::string&) { 27625b61b5e8SJason M. Bills if (ec) 27635b61b5e8SJason M. Bills { 27645b61b5e8SJason M. Bills messages::internalError(asyncResp->res); 27655b61b5e8SJason M. Bills return; 27665b61b5e8SJason M. Bills } 27675b61b5e8SJason M. Bills messages::success(asyncResp->res); 27685b61b5e8SJason M. Bills }, 2769002d39b4SEd Tanous crashdumpObject, crashdumpPath, deleteAllInterface, "DeleteAll"); 27707e860f15SJohn Edward Broadbent }); 27715b61b5e8SJason M. Bills } 27725b61b5e8SJason M. Bills 27738d1b46d7Szhanghch05 static void 27748d1b46d7Szhanghch05 logCrashdumpEntry(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp, 27758d1b46d7Szhanghch05 const std::string& logID, nlohmann::json& logEntryJson) 2776e855dd28SJason M. Bills { 2777043a0536SJohnathan Mantey auto getStoredLogCallback = 2778b9d36b47SEd Tanous [asyncResp, logID, 2779b9d36b47SEd Tanous &logEntryJson](const boost::system::error_code ec, 2780b9d36b47SEd Tanous const dbus::utility::DBusPropertiesMap& params) { 2781e855dd28SJason M. Bills if (ec) 2782e855dd28SJason M. Bills { 2783e855dd28SJason M. Bills BMCWEB_LOG_DEBUG << "failed to get log ec: " << ec.message(); 27841ddcf01aSJason M. Bills if (ec.value() == 27851ddcf01aSJason M. Bills boost::system::linux_error::bad_request_descriptor) 27861ddcf01aSJason M. Bills { 2787002d39b4SEd Tanous messages::resourceNotFound(asyncResp->res, "LogEntry", logID); 27881ddcf01aSJason M. Bills } 27891ddcf01aSJason M. Bills else 27901ddcf01aSJason M. Bills { 2791e855dd28SJason M. Bills messages::internalError(asyncResp->res); 27921ddcf01aSJason M. Bills } 2793e855dd28SJason M. Bills return; 2794e855dd28SJason M. Bills } 2795043a0536SJohnathan Mantey 2796043a0536SJohnathan Mantey std::string timestamp{}; 2797043a0536SJohnathan Mantey std::string filename{}; 2798043a0536SJohnathan Mantey std::string logfile{}; 27992c70f800SEd Tanous parseCrashdumpParameters(params, filename, timestamp, logfile); 2800043a0536SJohnathan Mantey 2801043a0536SJohnathan Mantey if (filename.empty() || timestamp.empty()) 2802e855dd28SJason M. Bills { 2803002d39b4SEd Tanous messages::resourceMissingAtURI(asyncResp->res, 2804002d39b4SEd Tanous crow::utility::urlFromPieces(logID)); 2805e855dd28SJason M. Bills return; 2806e855dd28SJason M. Bills } 2807e855dd28SJason M. Bills 2808043a0536SJohnathan Mantey std::string crashdumpURI = 2809e855dd28SJason M. Bills "/redfish/v1/Systems/system/LogServices/Crashdump/Entries/" + 2810043a0536SJohnathan Mantey logID + "/" + filename; 281184afc48bSJason M. Bills nlohmann::json::object_t logEntry; 281284afc48bSJason M. Bills logEntry["@odata.type"] = "#LogEntry.v1_7_0.LogEntry"; 281384afc48bSJason M. Bills logEntry["@odata.id"] = 281484afc48bSJason M. Bills "/redfish/v1/Systems/system/LogServices/Crashdump/Entries/" + logID; 281584afc48bSJason M. Bills logEntry["Name"] = "CPU Crashdump"; 281684afc48bSJason M. Bills logEntry["Id"] = logID; 281784afc48bSJason M. Bills logEntry["EntryType"] = "Oem"; 281884afc48bSJason M. Bills logEntry["AdditionalDataURI"] = std::move(crashdumpURI); 281984afc48bSJason M. Bills logEntry["DiagnosticDataType"] = "OEM"; 282084afc48bSJason M. Bills logEntry["OEMDiagnosticDataType"] = "PECICrashdump"; 282184afc48bSJason M. Bills logEntry["Created"] = std::move(timestamp); 28222b20ef6eSJason M. Bills 28232b20ef6eSJason M. Bills // If logEntryJson references an array of LogEntry resources 28242b20ef6eSJason M. Bills // ('Members' list), then push this as a new entry, otherwise set it 28252b20ef6eSJason M. Bills // directly 28262b20ef6eSJason M. Bills if (logEntryJson.is_array()) 28272b20ef6eSJason M. Bills { 28282b20ef6eSJason M. Bills logEntryJson.push_back(logEntry); 28292b20ef6eSJason M. Bills asyncResp->res.jsonValue["Members@odata.count"] = 28302b20ef6eSJason M. Bills logEntryJson.size(); 28312b20ef6eSJason M. Bills } 28322b20ef6eSJason M. Bills else 28332b20ef6eSJason M. Bills { 2834d405bb51SJason M. Bills logEntryJson.update(logEntry); 28352b20ef6eSJason M. Bills } 2836e855dd28SJason M. Bills }; 2837e855dd28SJason M. Bills crow::connections::systemBus->async_method_call( 28385b61b5e8SJason M. Bills std::move(getStoredLogCallback), crashdumpObject, 28395b61b5e8SJason M. Bills crashdumpPath + std::string("/") + logID, 2840043a0536SJohnathan Mantey "org.freedesktop.DBus.Properties", "GetAll", crashdumpInterface); 2841e855dd28SJason M. Bills } 2842e855dd28SJason M. Bills 28437e860f15SJohn Edward Broadbent inline void requestRoutesCrashdumpEntryCollection(App& app) 28441da66f75SEd Tanous { 28453946028dSAppaRao Puli // Note: Deviated from redfish privilege registry for GET & HEAD 28463946028dSAppaRao Puli // method for security reasons. 28471da66f75SEd Tanous /** 28481da66f75SEd Tanous * Functions triggers appropriate requests on DBus 28491da66f75SEd Tanous */ 28507e860f15SJohn Edward Broadbent BMCWEB_ROUTE(app, 28517e860f15SJohn Edward Broadbent "/redfish/v1/Systems/system/LogServices/Crashdump/Entries/") 2852ed398213SEd Tanous // This is incorrect, should be. 2853ed398213SEd Tanous //.privileges(redfish::privileges::postLogEntryCollection) 2854432a890cSEd Tanous .privileges({{"ConfigureComponents"}}) 2855002d39b4SEd Tanous .methods(boost::beast::http::verb::get)( 2856002d39b4SEd Tanous [&app](const crow::Request& req, 2857002d39b4SEd Tanous const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) { 28583ba00073SCarson Labrado if (!redfish::setUpRedfishRoute(app, req, asyncResp)) 285945ca1b86SEd Tanous { 286045ca1b86SEd Tanous return; 286145ca1b86SEd Tanous } 28622b20ef6eSJason M. Bills crow::connections::systemBus->async_method_call( 28632b20ef6eSJason M. Bills [asyncResp](const boost::system::error_code ec, 28642b20ef6eSJason M. Bills const std::vector<std::string>& resp) { 28651da66f75SEd Tanous if (ec) 28661da66f75SEd Tanous { 28671da66f75SEd Tanous if (ec.value() != 28681da66f75SEd Tanous boost::system::errc::no_such_file_or_directory) 28691da66f75SEd Tanous { 28701da66f75SEd Tanous BMCWEB_LOG_DEBUG << "failed to get entries ec: " 28711da66f75SEd Tanous << ec.message(); 2872f12894f8SJason M. Bills messages::internalError(asyncResp->res); 28731da66f75SEd Tanous return; 28741da66f75SEd Tanous } 28751da66f75SEd Tanous } 2876e1f26343SJason M. Bills asyncResp->res.jsonValue["@odata.type"] = 28771da66f75SEd Tanous "#LogEntryCollection.LogEntryCollection"; 28780f74e643SEd Tanous asyncResp->res.jsonValue["@odata.id"] = 2879424c4176SJason M. Bills "/redfish/v1/Systems/system/LogServices/Crashdump/Entries"; 2880002d39b4SEd Tanous asyncResp->res.jsonValue["Name"] = "Open BMC Crashdump Entries"; 2881e1f26343SJason M. Bills asyncResp->res.jsonValue["Description"] = 2882424c4176SJason M. Bills "Collection of Crashdump Entries"; 2883002d39b4SEd Tanous asyncResp->res.jsonValue["Members"] = nlohmann::json::array(); 2884a2dd60a6SBrandon Kim asyncResp->res.jsonValue["Members@odata.count"] = 0; 28852b20ef6eSJason M. Bills 28862b20ef6eSJason M. Bills for (const std::string& path : resp) 28871da66f75SEd Tanous { 28882b20ef6eSJason M. Bills const sdbusplus::message::object_path objPath(path); 2889e855dd28SJason M. Bills // Get the log ID 28902b20ef6eSJason M. Bills std::string logID = objPath.filename(); 28912b20ef6eSJason M. Bills if (logID.empty()) 28921da66f75SEd Tanous { 2893e855dd28SJason M. Bills continue; 28941da66f75SEd Tanous } 2895e855dd28SJason M. Bills // Add the log entry to the array 28962b20ef6eSJason M. Bills logCrashdumpEntry(asyncResp, logID, 28972b20ef6eSJason M. Bills asyncResp->res.jsonValue["Members"]); 28981da66f75SEd Tanous } 28992b20ef6eSJason M. Bills }, 29001da66f75SEd Tanous "xyz.openbmc_project.ObjectMapper", 29011da66f75SEd Tanous "/xyz/openbmc_project/object_mapper", 29021da66f75SEd Tanous "xyz.openbmc_project.ObjectMapper", "GetSubTreePaths", "", 0, 29035b61b5e8SJason M. Bills std::array<const char*, 1>{crashdumpInterface}); 29047e860f15SJohn Edward Broadbent }); 29051da66f75SEd Tanous } 29061da66f75SEd Tanous 29077e860f15SJohn Edward Broadbent inline void requestRoutesCrashdumpEntry(App& app) 29081da66f75SEd Tanous { 29093946028dSAppaRao Puli // Note: Deviated from redfish privilege registry for GET & HEAD 29103946028dSAppaRao Puli // method for security reasons. 29111da66f75SEd Tanous 29127e860f15SJohn Edward Broadbent BMCWEB_ROUTE( 29137e860f15SJohn Edward Broadbent app, "/redfish/v1/Systems/system/LogServices/Crashdump/Entries/<str>/") 2914ed398213SEd Tanous // this is incorrect, should be 2915ed398213SEd Tanous // .privileges(redfish::privileges::getLogEntry) 2916432a890cSEd Tanous .privileges({{"ConfigureComponents"}}) 29177e860f15SJohn Edward Broadbent .methods(boost::beast::http::verb::get)( 291845ca1b86SEd Tanous [&app](const crow::Request& req, 29197e860f15SJohn Edward Broadbent const std::shared_ptr<bmcweb::AsyncResp>& asyncResp, 29207e860f15SJohn Edward Broadbent const std::string& param) { 29213ba00073SCarson Labrado if (!redfish::setUpRedfishRoute(app, req, asyncResp)) 292245ca1b86SEd Tanous { 292345ca1b86SEd Tanous return; 292445ca1b86SEd Tanous } 29257e860f15SJohn Edward Broadbent const std::string& logID = param; 2926e855dd28SJason M. Bills logCrashdumpEntry(asyncResp, logID, asyncResp->res.jsonValue); 29277e860f15SJohn Edward Broadbent }); 2928e855dd28SJason M. Bills } 2929e855dd28SJason M. Bills 29307e860f15SJohn Edward Broadbent inline void requestRoutesCrashdumpFile(App& app) 2931e855dd28SJason M. Bills { 29323946028dSAppaRao Puli // Note: Deviated from redfish privilege registry for GET & HEAD 29333946028dSAppaRao Puli // method for security reasons. 29347e860f15SJohn Edward Broadbent BMCWEB_ROUTE( 29357e860f15SJohn Edward Broadbent app, 29367e860f15SJohn Edward Broadbent "/redfish/v1/Systems/system/LogServices/Crashdump/Entries/<str>/<str>/") 2937ed398213SEd Tanous .privileges(redfish::privileges::getLogEntry) 29387e860f15SJohn Edward Broadbent .methods(boost::beast::http::verb::get)( 2939a4ce114aSNan Zhou [](const crow::Request& req, 29407e860f15SJohn Edward Broadbent const std::shared_ptr<bmcweb::AsyncResp>& asyncResp, 29417e860f15SJohn Edward Broadbent const std::string& logID, const std::string& fileName) { 29422a9beeedSShounak Mitra // Do not call getRedfishRoute here since the crashdump file is not a 29432a9beeedSShounak Mitra // Redfish resource. 2944043a0536SJohnathan Mantey auto getStoredLogCallback = 2945002d39b4SEd Tanous [asyncResp, logID, fileName, url(boost::urls::url(req.urlView))]( 2946abf2add6SEd Tanous const boost::system::error_code ec, 2947002d39b4SEd Tanous const std::vector< 2948002d39b4SEd Tanous std::pair<std::string, dbus::utility::DbusVariantType>>& 29497e860f15SJohn Edward Broadbent resp) { 29501da66f75SEd Tanous if (ec) 29511da66f75SEd Tanous { 2952002d39b4SEd Tanous BMCWEB_LOG_DEBUG << "failed to get log ec: " << ec.message(); 2953f12894f8SJason M. Bills messages::internalError(asyncResp->res); 29541da66f75SEd Tanous return; 29551da66f75SEd Tanous } 2956e855dd28SJason M. Bills 2957043a0536SJohnathan Mantey std::string dbusFilename{}; 2958043a0536SJohnathan Mantey std::string dbusTimestamp{}; 2959043a0536SJohnathan Mantey std::string dbusFilepath{}; 2960043a0536SJohnathan Mantey 2961002d39b4SEd Tanous parseCrashdumpParameters(resp, dbusFilename, dbusTimestamp, 2962002d39b4SEd Tanous dbusFilepath); 2963043a0536SJohnathan Mantey 2964043a0536SJohnathan Mantey if (dbusFilename.empty() || dbusTimestamp.empty() || 2965043a0536SJohnathan Mantey dbusFilepath.empty()) 29661da66f75SEd Tanous { 2967ace85d60SEd Tanous messages::resourceMissingAtURI(asyncResp->res, url); 29681da66f75SEd Tanous return; 29691da66f75SEd Tanous } 2970e855dd28SJason M. Bills 2971043a0536SJohnathan Mantey // Verify the file name parameter is correct 2972043a0536SJohnathan Mantey if (fileName != dbusFilename) 2973043a0536SJohnathan Mantey { 2974ace85d60SEd Tanous messages::resourceMissingAtURI(asyncResp->res, url); 2975043a0536SJohnathan Mantey return; 2976043a0536SJohnathan Mantey } 2977043a0536SJohnathan Mantey 2978043a0536SJohnathan Mantey if (!std::filesystem::exists(dbusFilepath)) 2979043a0536SJohnathan Mantey { 2980ace85d60SEd Tanous messages::resourceMissingAtURI(asyncResp->res, url); 2981043a0536SJohnathan Mantey return; 2982043a0536SJohnathan Mantey } 2983002d39b4SEd Tanous std::ifstream ifs(dbusFilepath, std::ios::in | std::ios::binary); 2984002d39b4SEd Tanous asyncResp->res.body() = 2985002d39b4SEd Tanous std::string(std::istreambuf_iterator<char>{ifs}, {}); 2986043a0536SJohnathan Mantey 29877e860f15SJohn Edward Broadbent // Configure this to be a file download when accessed 29887e860f15SJohn Edward Broadbent // from a browser 2989d9f6c621SEd Tanous asyncResp->res.addHeader( 2990d9f6c621SEd Tanous boost::beast::http::field::content_disposition, "attachment"); 29911da66f75SEd Tanous }; 29921da66f75SEd Tanous crow::connections::systemBus->async_method_call( 29935b61b5e8SJason M. Bills std::move(getStoredLogCallback), crashdumpObject, 29945b61b5e8SJason M. Bills crashdumpPath + std::string("/") + logID, 2995002d39b4SEd Tanous "org.freedesktop.DBus.Properties", "GetAll", crashdumpInterface); 29967e860f15SJohn Edward Broadbent }); 29971da66f75SEd Tanous } 29981da66f75SEd Tanous 2999c5a4c82aSJason M. Bills enum class OEMDiagnosticType 3000c5a4c82aSJason M. Bills { 3001c5a4c82aSJason M. Bills onDemand, 3002c5a4c82aSJason M. Bills telemetry, 3003c5a4c82aSJason M. Bills invalid, 3004c5a4c82aSJason M. Bills }; 3005c5a4c82aSJason M. Bills 3006f7725d79SEd Tanous inline OEMDiagnosticType 3007f7725d79SEd Tanous getOEMDiagnosticType(const std::string_view& oemDiagStr) 3008c5a4c82aSJason M. Bills { 3009c5a4c82aSJason M. Bills if (oemDiagStr == "OnDemand") 3010c5a4c82aSJason M. Bills { 3011c5a4c82aSJason M. Bills return OEMDiagnosticType::onDemand; 3012c5a4c82aSJason M. Bills } 3013c5a4c82aSJason M. Bills if (oemDiagStr == "Telemetry") 3014c5a4c82aSJason M. Bills { 3015c5a4c82aSJason M. Bills return OEMDiagnosticType::telemetry; 3016c5a4c82aSJason M. Bills } 3017c5a4c82aSJason M. Bills 3018c5a4c82aSJason M. Bills return OEMDiagnosticType::invalid; 3019c5a4c82aSJason M. Bills } 3020c5a4c82aSJason M. Bills 30217e860f15SJohn Edward Broadbent inline void requestRoutesCrashdumpCollect(App& app) 30221da66f75SEd Tanous { 30233946028dSAppaRao Puli // Note: Deviated from redfish privilege registry for GET & HEAD 30243946028dSAppaRao Puli // method for security reasons. 30250fda0f12SGeorge Liu BMCWEB_ROUTE( 30260fda0f12SGeorge Liu app, 30270fda0f12SGeorge Liu "/redfish/v1/Systems/system/LogServices/Crashdump/Actions/LogService.CollectDiagnosticData/") 3028ed398213SEd Tanous // The below is incorrect; Should be ConfigureManager 3029ed398213SEd Tanous //.privileges(redfish::privileges::postLogService) 3030432a890cSEd Tanous .privileges({{"ConfigureComponents"}}) 3031002d39b4SEd Tanous .methods(boost::beast::http::verb::post)( 3032002d39b4SEd Tanous [&app](const crow::Request& req, 30337e860f15SJohn Edward Broadbent const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) { 30343ba00073SCarson Labrado if (!redfish::setUpRedfishRoute(app, req, asyncResp)) 303545ca1b86SEd Tanous { 303645ca1b86SEd Tanous return; 303745ca1b86SEd Tanous } 30388e6c099aSJason M. Bills std::string diagnosticDataType; 30398e6c099aSJason M. Bills std::string oemDiagnosticDataType; 304015ed6780SWilly Tu if (!redfish::json_util::readJsonAction( 3041002d39b4SEd Tanous req, asyncResp->res, "DiagnosticDataType", diagnosticDataType, 3042002d39b4SEd Tanous "OEMDiagnosticDataType", oemDiagnosticDataType)) 30438e6c099aSJason M. Bills { 30448e6c099aSJason M. Bills return; 30458e6c099aSJason M. Bills } 30468e6c099aSJason M. Bills 30478e6c099aSJason M. Bills if (diagnosticDataType != "OEM") 30488e6c099aSJason M. Bills { 30498e6c099aSJason M. Bills BMCWEB_LOG_ERROR 30508e6c099aSJason M. Bills << "Only OEM DiagnosticDataType supported for Crashdump"; 30518e6c099aSJason M. Bills messages::actionParameterValueFormatError( 30528e6c099aSJason M. Bills asyncResp->res, diagnosticDataType, "DiagnosticDataType", 30538e6c099aSJason M. Bills "CollectDiagnosticData"); 30548e6c099aSJason M. Bills return; 30558e6c099aSJason M. Bills } 30568e6c099aSJason M. Bills 3057c5a4c82aSJason M. Bills OEMDiagnosticType oemDiagType = 3058c5a4c82aSJason M. Bills getOEMDiagnosticType(oemDiagnosticDataType); 3059c5a4c82aSJason M. Bills 3060c5a4c82aSJason M. Bills std::string iface; 3061c5a4c82aSJason M. Bills std::string method; 3062c5a4c82aSJason M. Bills std::string taskMatchStr; 3063c5a4c82aSJason M. Bills if (oemDiagType == OEMDiagnosticType::onDemand) 3064c5a4c82aSJason M. Bills { 3065c5a4c82aSJason M. Bills iface = crashdumpOnDemandInterface; 3066c5a4c82aSJason M. Bills method = "GenerateOnDemandLog"; 3067c5a4c82aSJason M. Bills taskMatchStr = "type='signal'," 3068c5a4c82aSJason M. Bills "interface='org.freedesktop.DBus.Properties'," 3069c5a4c82aSJason M. Bills "member='PropertiesChanged'," 3070c5a4c82aSJason M. Bills "arg0namespace='com.intel.crashdump'"; 3071c5a4c82aSJason M. Bills } 3072c5a4c82aSJason M. Bills else if (oemDiagType == OEMDiagnosticType::telemetry) 3073c5a4c82aSJason M. Bills { 3074c5a4c82aSJason M. Bills iface = crashdumpTelemetryInterface; 3075c5a4c82aSJason M. Bills method = "GenerateTelemetryLog"; 3076c5a4c82aSJason M. Bills taskMatchStr = "type='signal'," 3077c5a4c82aSJason M. Bills "interface='org.freedesktop.DBus.Properties'," 3078c5a4c82aSJason M. Bills "member='PropertiesChanged'," 3079c5a4c82aSJason M. Bills "arg0namespace='com.intel.crashdump'"; 3080c5a4c82aSJason M. Bills } 3081c5a4c82aSJason M. Bills else 3082c5a4c82aSJason M. Bills { 3083c5a4c82aSJason M. Bills BMCWEB_LOG_ERROR << "Unsupported OEMDiagnosticDataType: " 3084c5a4c82aSJason M. Bills << oemDiagnosticDataType; 3085c5a4c82aSJason M. Bills messages::actionParameterValueFormatError( 3086002d39b4SEd Tanous asyncResp->res, oemDiagnosticDataType, "OEMDiagnosticDataType", 3087002d39b4SEd Tanous "CollectDiagnosticData"); 3088c5a4c82aSJason M. Bills return; 3089c5a4c82aSJason M. Bills } 3090c5a4c82aSJason M. Bills 3091c5a4c82aSJason M. Bills auto collectCrashdumpCallback = 3092c5a4c82aSJason M. Bills [asyncResp, payload(task::Payload(req)), 3093c5a4c82aSJason M. Bills taskMatchStr](const boost::system::error_code ec, 309498be3e39SEd Tanous const std::string&) mutable { 30951da66f75SEd Tanous if (ec) 30961da66f75SEd Tanous { 3097002d39b4SEd Tanous if (ec.value() == boost::system::errc::operation_not_supported) 30981da66f75SEd Tanous { 3099f12894f8SJason M. Bills messages::resourceInStandby(asyncResp->res); 31001da66f75SEd Tanous } 31014363d3b2SJason M. Bills else if (ec.value() == 31024363d3b2SJason M. Bills boost::system::errc::device_or_resource_busy) 31034363d3b2SJason M. Bills { 3104002d39b4SEd Tanous messages::serviceTemporarilyUnavailable(asyncResp->res, 3105002d39b4SEd Tanous "60"); 31064363d3b2SJason M. Bills } 31071da66f75SEd Tanous else 31081da66f75SEd Tanous { 3109f12894f8SJason M. Bills messages::internalError(asyncResp->res); 31101da66f75SEd Tanous } 31111da66f75SEd Tanous return; 31121da66f75SEd Tanous } 3113002d39b4SEd Tanous std::shared_ptr<task::TaskData> task = task::TaskData::createTask( 311459d494eeSPatrick Williams [](boost::system::error_code err, sdbusplus::message_t&, 3115002d39b4SEd Tanous const std::shared_ptr<task::TaskData>& taskData) { 311666afe4faSJames Feist if (!err) 311766afe4faSJames Feist { 3118002d39b4SEd Tanous taskData->messages.emplace_back(messages::taskCompletedOK( 3119e5d5006bSJames Feist std::to_string(taskData->index))); 3120831d6b09SJames Feist taskData->state = "Completed"; 312166afe4faSJames Feist } 312232898ceaSJames Feist return task::completed; 312366afe4faSJames Feist }, 3124c5a4c82aSJason M. Bills taskMatchStr); 3125c5a4c82aSJason M. Bills 312646229577SJames Feist task->startTimer(std::chrono::minutes(5)); 312746229577SJames Feist task->populateResp(asyncResp->res); 312898be3e39SEd Tanous task->payload.emplace(std::move(payload)); 31291da66f75SEd Tanous }; 31308e6c099aSJason M. Bills 31311da66f75SEd Tanous crow::connections::systemBus->async_method_call( 3132002d39b4SEd Tanous std::move(collectCrashdumpCallback), crashdumpObject, crashdumpPath, 3133002d39b4SEd Tanous iface, method); 31347e860f15SJohn Edward Broadbent }); 31356eda7685SKenny L. Ku } 31366eda7685SKenny L. Ku 3137cb92c03bSAndrew Geissler /** 3138cb92c03bSAndrew Geissler * DBusLogServiceActionsClear class supports POST method for ClearLog action. 3139cb92c03bSAndrew Geissler */ 31407e860f15SJohn Edward Broadbent inline void requestRoutesDBusLogServiceActionsClear(App& app) 3141cb92c03bSAndrew Geissler { 3142cb92c03bSAndrew Geissler /** 3143cb92c03bSAndrew Geissler * Function handles POST method request. 3144cb92c03bSAndrew Geissler * The Clear Log actions does not require any parameter.The action deletes 3145cb92c03bSAndrew Geissler * all entries found in the Entries collection for this Log Service. 3146cb92c03bSAndrew Geissler */ 31477e860f15SJohn Edward Broadbent 31480fda0f12SGeorge Liu BMCWEB_ROUTE( 31490fda0f12SGeorge Liu app, 31500fda0f12SGeorge Liu "/redfish/v1/Systems/system/LogServices/EventLog/Actions/LogService.ClearLog/") 3151ed398213SEd Tanous .privileges(redfish::privileges::postLogService) 31527e860f15SJohn Edward Broadbent .methods(boost::beast::http::verb::post)( 315345ca1b86SEd Tanous [&app](const crow::Request& req, 31547e860f15SJohn Edward Broadbent const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) { 31553ba00073SCarson Labrado if (!redfish::setUpRedfishRoute(app, req, asyncResp)) 315645ca1b86SEd Tanous { 315745ca1b86SEd Tanous return; 315845ca1b86SEd Tanous } 3159cb92c03bSAndrew Geissler BMCWEB_LOG_DEBUG << "Do delete all entries."; 3160cb92c03bSAndrew Geissler 3161cb92c03bSAndrew Geissler // Process response from Logging service. 3162002d39b4SEd Tanous auto respHandler = [asyncResp](const boost::system::error_code ec) { 3163002d39b4SEd Tanous BMCWEB_LOG_DEBUG << "doClearLog resp_handler callback: Done"; 3164cb92c03bSAndrew Geissler if (ec) 3165cb92c03bSAndrew Geissler { 3166cb92c03bSAndrew Geissler // TODO Handle for specific error code 3167002d39b4SEd Tanous BMCWEB_LOG_ERROR << "doClearLog resp_handler got error " << ec; 3168cb92c03bSAndrew Geissler asyncResp->res.result( 3169cb92c03bSAndrew Geissler boost::beast::http::status::internal_server_error); 3170cb92c03bSAndrew Geissler return; 3171cb92c03bSAndrew Geissler } 3172cb92c03bSAndrew Geissler 3173002d39b4SEd Tanous asyncResp->res.result(boost::beast::http::status::no_content); 3174cb92c03bSAndrew Geissler }; 3175cb92c03bSAndrew Geissler 3176cb92c03bSAndrew Geissler // Make call to Logging service to request Clear Log 3177cb92c03bSAndrew Geissler crow::connections::systemBus->async_method_call( 31782c70f800SEd Tanous respHandler, "xyz.openbmc_project.Logging", 3179cb92c03bSAndrew Geissler "/xyz/openbmc_project/logging", 3180cb92c03bSAndrew Geissler "xyz.openbmc_project.Collection.DeleteAll", "DeleteAll"); 31817e860f15SJohn Edward Broadbent }); 3182cb92c03bSAndrew Geissler } 3183a3316fc6SZhikuiRen 3184a3316fc6SZhikuiRen /**************************************************** 3185a3316fc6SZhikuiRen * Redfish PostCode interfaces 3186a3316fc6SZhikuiRen * using DBUS interface: getPostCodesTS 3187a3316fc6SZhikuiRen ******************************************************/ 31887e860f15SJohn Edward Broadbent inline void requestRoutesPostCodesLogService(App& app) 3189a3316fc6SZhikuiRen { 31907e860f15SJohn Edward Broadbent BMCWEB_ROUTE(app, "/redfish/v1/Systems/system/LogServices/PostCodes/") 3191ed398213SEd Tanous .privileges(redfish::privileges::getLogService) 3192002d39b4SEd Tanous .methods(boost::beast::http::verb::get)( 3193002d39b4SEd Tanous [&app](const crow::Request& req, 3194002d39b4SEd Tanous const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) { 31953ba00073SCarson Labrado if (!redfish::setUpRedfishRoute(app, req, asyncResp)) 319645ca1b86SEd Tanous { 319745ca1b86SEd Tanous return; 319845ca1b86SEd Tanous } 31991476687dSEd Tanous 32001476687dSEd Tanous asyncResp->res.jsonValue["@odata.id"] = 32011476687dSEd Tanous "/redfish/v1/Systems/system/LogServices/PostCodes"; 32021476687dSEd Tanous asyncResp->res.jsonValue["@odata.type"] = 32031476687dSEd Tanous "#LogService.v1_1_0.LogService"; 32041476687dSEd Tanous asyncResp->res.jsonValue["Name"] = "POST Code Log Service"; 32051476687dSEd Tanous asyncResp->res.jsonValue["Description"] = "POST Code Log Service"; 32061476687dSEd Tanous asyncResp->res.jsonValue["Id"] = "BIOS POST Code Log"; 32071476687dSEd Tanous asyncResp->res.jsonValue["OverWritePolicy"] = "WrapsWhenFull"; 32081476687dSEd Tanous asyncResp->res.jsonValue["Entries"]["@odata.id"] = 32091476687dSEd Tanous "/redfish/v1/Systems/system/LogServices/PostCodes/Entries"; 32107c8c4058STejas Patil 32117c8c4058STejas Patil std::pair<std::string, std::string> redfishDateTimeOffset = 32122b82937eSEd Tanous redfish::time_utils::getDateTimeOffsetNow(); 32130fda0f12SGeorge Liu asyncResp->res.jsonValue["DateTime"] = redfishDateTimeOffset.first; 32147c8c4058STejas Patil asyncResp->res.jsonValue["DateTimeLocalOffset"] = 32157c8c4058STejas Patil redfishDateTimeOffset.second; 32167c8c4058STejas Patil 3217a3316fc6SZhikuiRen asyncResp->res.jsonValue["Actions"]["#LogService.ClearLog"] = { 32187e860f15SJohn Edward Broadbent {"target", 32190fda0f12SGeorge Liu "/redfish/v1/Systems/system/LogServices/PostCodes/Actions/LogService.ClearLog"}}; 32207e860f15SJohn Edward Broadbent }); 3221a3316fc6SZhikuiRen } 3222a3316fc6SZhikuiRen 32237e860f15SJohn Edward Broadbent inline void requestRoutesPostCodesClear(App& app) 3224a3316fc6SZhikuiRen { 32250fda0f12SGeorge Liu BMCWEB_ROUTE( 32260fda0f12SGeorge Liu app, 32270fda0f12SGeorge Liu "/redfish/v1/Systems/system/LogServices/PostCodes/Actions/LogService.ClearLog/") 3228ed398213SEd Tanous // The following privilege is incorrect; It should be ConfigureManager 3229ed398213SEd Tanous //.privileges(redfish::privileges::postLogService) 3230432a890cSEd Tanous .privileges({{"ConfigureComponents"}}) 32317e860f15SJohn Edward Broadbent .methods(boost::beast::http::verb::post)( 323245ca1b86SEd Tanous [&app](const crow::Request& req, 32337e860f15SJohn Edward Broadbent const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) { 32343ba00073SCarson Labrado if (!redfish::setUpRedfishRoute(app, req, asyncResp)) 323545ca1b86SEd Tanous { 323645ca1b86SEd Tanous return; 323745ca1b86SEd Tanous } 3238a3316fc6SZhikuiRen BMCWEB_LOG_DEBUG << "Do delete all postcodes entries."; 3239a3316fc6SZhikuiRen 3240a3316fc6SZhikuiRen // Make call to post-code service to request clear all 3241a3316fc6SZhikuiRen crow::connections::systemBus->async_method_call( 3242a3316fc6SZhikuiRen [asyncResp](const boost::system::error_code ec) { 3243a3316fc6SZhikuiRen if (ec) 3244a3316fc6SZhikuiRen { 3245a3316fc6SZhikuiRen // TODO Handle for specific error code 3246002d39b4SEd Tanous BMCWEB_LOG_ERROR << "doClearPostCodes resp_handler got error " 32477e860f15SJohn Edward Broadbent << ec; 3248002d39b4SEd Tanous asyncResp->res.result( 3249002d39b4SEd Tanous boost::beast::http::status::internal_server_error); 3250a3316fc6SZhikuiRen messages::internalError(asyncResp->res); 3251a3316fc6SZhikuiRen return; 3252a3316fc6SZhikuiRen } 3253a3316fc6SZhikuiRen }, 325415124765SJonathan Doman "xyz.openbmc_project.State.Boot.PostCode0", 325515124765SJonathan Doman "/xyz/openbmc_project/State/Boot/PostCode0", 3256a3316fc6SZhikuiRen "xyz.openbmc_project.Collection.DeleteAll", "DeleteAll"); 32577e860f15SJohn Edward Broadbent }); 3258a3316fc6SZhikuiRen } 3259a3316fc6SZhikuiRen 3260a3316fc6SZhikuiRen static void fillPostCodeEntry( 32618d1b46d7Szhanghch05 const std::shared_ptr<bmcweb::AsyncResp>& aResp, 32626c9a279eSManojkiran Eda const boost::container::flat_map< 32636c9a279eSManojkiran Eda uint64_t, std::tuple<uint64_t, std::vector<uint8_t>>>& postcode, 3264a3316fc6SZhikuiRen const uint16_t bootIndex, const uint64_t codeIndex = 0, 3265a3316fc6SZhikuiRen const uint64_t skip = 0, const uint64_t top = 0) 3266a3316fc6SZhikuiRen { 3267a3316fc6SZhikuiRen // Get the Message from the MessageRegistry 3268fffb8c1fSEd Tanous const registries::Message* message = 3269fffb8c1fSEd Tanous registries::getMessage("OpenBMC.0.2.BIOSPOSTCode"); 3270a3316fc6SZhikuiRen 3271a3316fc6SZhikuiRen uint64_t currentCodeIndex = 0; 3272a3316fc6SZhikuiRen nlohmann::json& logEntryArray = aResp->res.jsonValue["Members"]; 3273a3316fc6SZhikuiRen 3274a3316fc6SZhikuiRen uint64_t firstCodeTimeUs = 0; 32756c9a279eSManojkiran Eda for (const std::pair<uint64_t, std::tuple<uint64_t, std::vector<uint8_t>>>& 32766c9a279eSManojkiran Eda code : postcode) 3277a3316fc6SZhikuiRen { 3278a3316fc6SZhikuiRen currentCodeIndex++; 3279a3316fc6SZhikuiRen std::string postcodeEntryID = 3280a3316fc6SZhikuiRen "B" + std::to_string(bootIndex) + "-" + 3281a3316fc6SZhikuiRen std::to_string(currentCodeIndex); // 1 based index in EntryID string 3282a3316fc6SZhikuiRen 3283a3316fc6SZhikuiRen uint64_t usecSinceEpoch = code.first; 3284a3316fc6SZhikuiRen uint64_t usTimeOffset = 0; 3285a3316fc6SZhikuiRen 3286a3316fc6SZhikuiRen if (1 == currentCodeIndex) 3287a3316fc6SZhikuiRen { // already incremented 3288a3316fc6SZhikuiRen firstCodeTimeUs = code.first; 3289a3316fc6SZhikuiRen } 3290a3316fc6SZhikuiRen else 3291a3316fc6SZhikuiRen { 3292a3316fc6SZhikuiRen usTimeOffset = code.first - firstCodeTimeUs; 3293a3316fc6SZhikuiRen } 3294a3316fc6SZhikuiRen 3295a3316fc6SZhikuiRen // skip if no specific codeIndex is specified and currentCodeIndex does 3296a3316fc6SZhikuiRen // not fall between top and skip 3297a3316fc6SZhikuiRen if ((codeIndex == 0) && 3298a3316fc6SZhikuiRen (currentCodeIndex <= skip || currentCodeIndex > top)) 3299a3316fc6SZhikuiRen { 3300a3316fc6SZhikuiRen continue; 3301a3316fc6SZhikuiRen } 3302a3316fc6SZhikuiRen 33034e0453b1SGunnar Mills // skip if a specific codeIndex is specified and does not match the 3304a3316fc6SZhikuiRen // currentIndex 3305a3316fc6SZhikuiRen if ((codeIndex > 0) && (currentCodeIndex != codeIndex)) 3306a3316fc6SZhikuiRen { 3307a3316fc6SZhikuiRen // This is done for simplicity. 1st entry is needed to calculate 3308a3316fc6SZhikuiRen // time offset. To improve efficiency, one can get to the entry 3309a3316fc6SZhikuiRen // directly (possibly with flatmap's nth method) 3310a3316fc6SZhikuiRen continue; 3311a3316fc6SZhikuiRen } 3312a3316fc6SZhikuiRen 3313a3316fc6SZhikuiRen // currentCodeIndex is within top and skip or equal to specified code 3314a3316fc6SZhikuiRen // index 3315a3316fc6SZhikuiRen 3316a3316fc6SZhikuiRen // Get the Created time from the timestamp 3317a3316fc6SZhikuiRen std::string entryTimeStr; 33181d8782e7SNan Zhou entryTimeStr = 33192b82937eSEd Tanous redfish::time_utils::getDateTimeUint(usecSinceEpoch / 1000 / 1000); 3320a3316fc6SZhikuiRen 3321a3316fc6SZhikuiRen // assemble messageArgs: BootIndex, TimeOffset(100us), PostCode(hex) 3322a3316fc6SZhikuiRen std::ostringstream hexCode; 3323a3316fc6SZhikuiRen hexCode << "0x" << std::setfill('0') << std::setw(2) << std::hex 33246c9a279eSManojkiran Eda << std::get<0>(code.second); 3325a3316fc6SZhikuiRen std::ostringstream timeOffsetStr; 3326a3316fc6SZhikuiRen // Set Fixed -Point Notation 3327a3316fc6SZhikuiRen timeOffsetStr << std::fixed; 3328a3316fc6SZhikuiRen // Set precision to 4 digits 3329a3316fc6SZhikuiRen timeOffsetStr << std::setprecision(4); 3330a3316fc6SZhikuiRen // Add double to stream 3331a3316fc6SZhikuiRen timeOffsetStr << static_cast<double>(usTimeOffset) / 1000 / 1000; 3332a3316fc6SZhikuiRen std::vector<std::string> messageArgs = { 3333a3316fc6SZhikuiRen std::to_string(bootIndex), timeOffsetStr.str(), hexCode.str()}; 3334a3316fc6SZhikuiRen 3335a3316fc6SZhikuiRen // Get MessageArgs template from message registry 3336a3316fc6SZhikuiRen std::string msg; 3337a3316fc6SZhikuiRen if (message != nullptr) 3338a3316fc6SZhikuiRen { 3339a3316fc6SZhikuiRen msg = message->message; 3340a3316fc6SZhikuiRen 3341a3316fc6SZhikuiRen // fill in this post code value 3342a3316fc6SZhikuiRen int i = 0; 3343a3316fc6SZhikuiRen for (const std::string& messageArg : messageArgs) 3344a3316fc6SZhikuiRen { 3345a3316fc6SZhikuiRen std::string argStr = "%" + std::to_string(++i); 3346a3316fc6SZhikuiRen size_t argPos = msg.find(argStr); 3347a3316fc6SZhikuiRen if (argPos != std::string::npos) 3348a3316fc6SZhikuiRen { 3349a3316fc6SZhikuiRen msg.replace(argPos, argStr.length(), messageArg); 3350a3316fc6SZhikuiRen } 3351a3316fc6SZhikuiRen } 3352a3316fc6SZhikuiRen } 3353a3316fc6SZhikuiRen 3354d4342a92STim Lee // Get Severity template from message registry 3355d4342a92STim Lee std::string severity; 3356d4342a92STim Lee if (message != nullptr) 3357d4342a92STim Lee { 33585f2b84eeSEd Tanous severity = message->messageSeverity; 3359d4342a92STim Lee } 3360d4342a92STim Lee 3361a3316fc6SZhikuiRen // add to AsyncResp 3362a3316fc6SZhikuiRen logEntryArray.push_back({}); 3363a3316fc6SZhikuiRen nlohmann::json& bmcLogEntry = logEntryArray.back(); 336484afc48bSJason M. Bills bmcLogEntry["@odata.type"] = "#LogEntry.v1_8_0.LogEntry"; 336584afc48bSJason M. Bills bmcLogEntry["@odata.id"] = 33660fda0f12SGeorge Liu "/redfish/v1/Systems/system/LogServices/PostCodes/Entries/" + 336784afc48bSJason M. Bills postcodeEntryID; 336884afc48bSJason M. Bills bmcLogEntry["Name"] = "POST Code Log Entry"; 336984afc48bSJason M. Bills bmcLogEntry["Id"] = postcodeEntryID; 337084afc48bSJason M. Bills bmcLogEntry["Message"] = std::move(msg); 337184afc48bSJason M. Bills bmcLogEntry["MessageId"] = "OpenBMC.0.2.BIOSPOSTCode"; 337284afc48bSJason M. Bills bmcLogEntry["MessageArgs"] = std::move(messageArgs); 337384afc48bSJason M. Bills bmcLogEntry["EntryType"] = "Event"; 337484afc48bSJason M. Bills bmcLogEntry["Severity"] = std::move(severity); 337584afc48bSJason M. Bills bmcLogEntry["Created"] = entryTimeStr; 3376647b3cdcSGeorge Liu if (!std::get<std::vector<uint8_t>>(code.second).empty()) 3377647b3cdcSGeorge Liu { 3378647b3cdcSGeorge Liu bmcLogEntry["AdditionalDataURI"] = 3379647b3cdcSGeorge Liu "/redfish/v1/Systems/system/LogServices/PostCodes/Entries/" + 3380647b3cdcSGeorge Liu postcodeEntryID + "/attachment"; 3381647b3cdcSGeorge Liu } 3382a3316fc6SZhikuiRen } 3383a3316fc6SZhikuiRen } 3384a3316fc6SZhikuiRen 33858d1b46d7Szhanghch05 static void getPostCodeForEntry(const std::shared_ptr<bmcweb::AsyncResp>& aResp, 3386a3316fc6SZhikuiRen const uint16_t bootIndex, 3387a3316fc6SZhikuiRen const uint64_t codeIndex) 3388a3316fc6SZhikuiRen { 3389a3316fc6SZhikuiRen crow::connections::systemBus->async_method_call( 33906c9a279eSManojkiran Eda [aResp, bootIndex, 33916c9a279eSManojkiran Eda codeIndex](const boost::system::error_code ec, 33926c9a279eSManojkiran Eda const boost::container::flat_map< 33936c9a279eSManojkiran Eda uint64_t, std::tuple<uint64_t, std::vector<uint8_t>>>& 33946c9a279eSManojkiran Eda postcode) { 3395a3316fc6SZhikuiRen if (ec) 3396a3316fc6SZhikuiRen { 3397a3316fc6SZhikuiRen BMCWEB_LOG_DEBUG << "DBUS POST CODE PostCode response error"; 3398a3316fc6SZhikuiRen messages::internalError(aResp->res); 3399a3316fc6SZhikuiRen return; 3400a3316fc6SZhikuiRen } 3401a3316fc6SZhikuiRen 3402a3316fc6SZhikuiRen // skip the empty postcode boots 3403a3316fc6SZhikuiRen if (postcode.empty()) 3404a3316fc6SZhikuiRen { 3405a3316fc6SZhikuiRen return; 3406a3316fc6SZhikuiRen } 3407a3316fc6SZhikuiRen 3408a3316fc6SZhikuiRen fillPostCodeEntry(aResp, postcode, bootIndex, codeIndex); 3409a3316fc6SZhikuiRen 3410a3316fc6SZhikuiRen aResp->res.jsonValue["Members@odata.count"] = 3411a3316fc6SZhikuiRen aResp->res.jsonValue["Members"].size(); 3412a3316fc6SZhikuiRen }, 341315124765SJonathan Doman "xyz.openbmc_project.State.Boot.PostCode0", 341415124765SJonathan Doman "/xyz/openbmc_project/State/Boot/PostCode0", 3415a3316fc6SZhikuiRen "xyz.openbmc_project.State.Boot.PostCode", "GetPostCodesWithTimeStamp", 3416a3316fc6SZhikuiRen bootIndex); 3417a3316fc6SZhikuiRen } 3418a3316fc6SZhikuiRen 34198d1b46d7Szhanghch05 static void getPostCodeForBoot(const std::shared_ptr<bmcweb::AsyncResp>& aResp, 3420a3316fc6SZhikuiRen const uint16_t bootIndex, 3421a3316fc6SZhikuiRen const uint16_t bootCount, 34223648c8beSEd Tanous const uint64_t entryCount, size_t skip, 34233648c8beSEd Tanous size_t top) 3424a3316fc6SZhikuiRen { 3425a3316fc6SZhikuiRen crow::connections::systemBus->async_method_call( 3426a3316fc6SZhikuiRen [aResp, bootIndex, bootCount, entryCount, skip, 3427a3316fc6SZhikuiRen top](const boost::system::error_code ec, 34286c9a279eSManojkiran Eda const boost::container::flat_map< 34296c9a279eSManojkiran Eda uint64_t, std::tuple<uint64_t, std::vector<uint8_t>>>& 34306c9a279eSManojkiran Eda postcode) { 3431a3316fc6SZhikuiRen if (ec) 3432a3316fc6SZhikuiRen { 3433a3316fc6SZhikuiRen BMCWEB_LOG_DEBUG << "DBUS POST CODE PostCode response error"; 3434a3316fc6SZhikuiRen messages::internalError(aResp->res); 3435a3316fc6SZhikuiRen return; 3436a3316fc6SZhikuiRen } 3437a3316fc6SZhikuiRen 3438a3316fc6SZhikuiRen uint64_t endCount = entryCount; 3439a3316fc6SZhikuiRen if (!postcode.empty()) 3440a3316fc6SZhikuiRen { 3441a3316fc6SZhikuiRen endCount = entryCount + postcode.size(); 34423648c8beSEd Tanous if (skip < endCount && (top + skip) > entryCount) 3443a3316fc6SZhikuiRen { 34443648c8beSEd Tanous uint64_t thisBootSkip = 34453648c8beSEd Tanous std::max(static_cast<uint64_t>(skip), entryCount) - 34463648c8beSEd Tanous entryCount; 3447a3316fc6SZhikuiRen uint64_t thisBootTop = 34483648c8beSEd Tanous std::min(static_cast<uint64_t>(top + skip), endCount) - 34493648c8beSEd Tanous entryCount; 3450a3316fc6SZhikuiRen 3451002d39b4SEd Tanous fillPostCodeEntry(aResp, postcode, bootIndex, 0, thisBootSkip, 3452002d39b4SEd Tanous thisBootTop); 3453a3316fc6SZhikuiRen } 3454a3316fc6SZhikuiRen aResp->res.jsonValue["Members@odata.count"] = endCount; 3455a3316fc6SZhikuiRen } 3456a3316fc6SZhikuiRen 3457a3316fc6SZhikuiRen // continue to previous bootIndex 3458a3316fc6SZhikuiRen if (bootIndex < bootCount) 3459a3316fc6SZhikuiRen { 3460a3316fc6SZhikuiRen getPostCodeForBoot(aResp, static_cast<uint16_t>(bootIndex + 1), 3461a3316fc6SZhikuiRen bootCount, endCount, skip, top); 3462a3316fc6SZhikuiRen } 346381584abeSJiaqing Zhao else if (skip + top < endCount) 3464a3316fc6SZhikuiRen { 3465a3316fc6SZhikuiRen aResp->res.jsonValue["Members@odata.nextLink"] = 34660fda0f12SGeorge Liu "/redfish/v1/Systems/system/LogServices/PostCodes/Entries?$skip=" + 3467a3316fc6SZhikuiRen std::to_string(skip + top); 3468a3316fc6SZhikuiRen } 3469a3316fc6SZhikuiRen }, 347015124765SJonathan Doman "xyz.openbmc_project.State.Boot.PostCode0", 347115124765SJonathan Doman "/xyz/openbmc_project/State/Boot/PostCode0", 3472a3316fc6SZhikuiRen "xyz.openbmc_project.State.Boot.PostCode", "GetPostCodesWithTimeStamp", 3473a3316fc6SZhikuiRen bootIndex); 3474a3316fc6SZhikuiRen } 3475a3316fc6SZhikuiRen 34768d1b46d7Szhanghch05 static void 34778d1b46d7Szhanghch05 getCurrentBootNumber(const std::shared_ptr<bmcweb::AsyncResp>& aResp, 34783648c8beSEd Tanous size_t skip, size_t top) 3479a3316fc6SZhikuiRen { 3480a3316fc6SZhikuiRen uint64_t entryCount = 0; 34811e1e598dSJonathan Doman sdbusplus::asio::getProperty<uint16_t>( 34821e1e598dSJonathan Doman *crow::connections::systemBus, 34831e1e598dSJonathan Doman "xyz.openbmc_project.State.Boot.PostCode0", 34841e1e598dSJonathan Doman "/xyz/openbmc_project/State/Boot/PostCode0", 34851e1e598dSJonathan Doman "xyz.openbmc_project.State.Boot.PostCode", "CurrentBootCycleCount", 34861e1e598dSJonathan Doman [aResp, entryCount, skip, top](const boost::system::error_code ec, 34871e1e598dSJonathan Doman const uint16_t bootCount) { 3488a3316fc6SZhikuiRen if (ec) 3489a3316fc6SZhikuiRen { 3490a3316fc6SZhikuiRen BMCWEB_LOG_DEBUG << "DBUS response error " << ec; 3491a3316fc6SZhikuiRen messages::internalError(aResp->res); 3492a3316fc6SZhikuiRen return; 3493a3316fc6SZhikuiRen } 34941e1e598dSJonathan Doman getPostCodeForBoot(aResp, 1, bootCount, entryCount, skip, top); 34951e1e598dSJonathan Doman }); 3496a3316fc6SZhikuiRen } 3497a3316fc6SZhikuiRen 34987e860f15SJohn Edward Broadbent inline void requestRoutesPostCodesEntryCollection(App& app) 3499a3316fc6SZhikuiRen { 35007e860f15SJohn Edward Broadbent BMCWEB_ROUTE(app, 35017e860f15SJohn Edward Broadbent "/redfish/v1/Systems/system/LogServices/PostCodes/Entries/") 3502ed398213SEd Tanous .privileges(redfish::privileges::getLogEntryCollection) 35037e860f15SJohn Edward Broadbent .methods(boost::beast::http::verb::get)( 350445ca1b86SEd Tanous [&app](const crow::Request& req, 35057e860f15SJohn Edward Broadbent const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) { 3506c937d2bfSEd Tanous query_param::QueryCapabilities capabilities = { 3507c937d2bfSEd Tanous .canDelegateTop = true, 3508c937d2bfSEd Tanous .canDelegateSkip = true, 3509c937d2bfSEd Tanous }; 3510c937d2bfSEd Tanous query_param::Query delegatedQuery; 3511c937d2bfSEd Tanous if (!redfish::setUpRedfishRouteWithDelegation( 35123ba00073SCarson Labrado app, req, asyncResp, delegatedQuery, capabilities)) 351345ca1b86SEd Tanous { 351445ca1b86SEd Tanous return; 351545ca1b86SEd Tanous } 3516a3316fc6SZhikuiRen asyncResp->res.jsonValue["@odata.type"] = 3517a3316fc6SZhikuiRen "#LogEntryCollection.LogEntryCollection"; 3518a3316fc6SZhikuiRen asyncResp->res.jsonValue["@odata.id"] = 3519a3316fc6SZhikuiRen "/redfish/v1/Systems/system/LogServices/PostCodes/Entries"; 3520a3316fc6SZhikuiRen asyncResp->res.jsonValue["Name"] = "BIOS POST Code Log Entries"; 3521a3316fc6SZhikuiRen asyncResp->res.jsonValue["Description"] = 3522a3316fc6SZhikuiRen "Collection of POST Code Log Entries"; 3523a3316fc6SZhikuiRen asyncResp->res.jsonValue["Members"] = nlohmann::json::array(); 3524a3316fc6SZhikuiRen asyncResp->res.jsonValue["Members@odata.count"] = 0; 35253648c8beSEd Tanous size_t skip = delegatedQuery.skip.value_or(0); 35265143f7a5SJiaqing Zhao size_t top = delegatedQuery.top.value_or(query_param::Query::maxTop); 35273648c8beSEd Tanous getCurrentBootNumber(asyncResp, skip, top); 35287e860f15SJohn Edward Broadbent }); 3529a3316fc6SZhikuiRen } 3530a3316fc6SZhikuiRen 3531647b3cdcSGeorge Liu /** 3532647b3cdcSGeorge Liu * @brief Parse post code ID and get the current value and index value 3533647b3cdcSGeorge Liu * eg: postCodeID=B1-2, currentValue=1, index=2 3534647b3cdcSGeorge Liu * 3535647b3cdcSGeorge Liu * @param[in] postCodeID Post Code ID 3536647b3cdcSGeorge Liu * @param[out] currentValue Current value 3537647b3cdcSGeorge Liu * @param[out] index Index value 3538647b3cdcSGeorge Liu * 3539647b3cdcSGeorge Liu * @return bool true if the parsing is successful, false the parsing fails 3540647b3cdcSGeorge Liu */ 3541647b3cdcSGeorge Liu inline static bool parsePostCode(const std::string& postCodeID, 3542647b3cdcSGeorge Liu uint64_t& currentValue, uint16_t& index) 3543647b3cdcSGeorge Liu { 3544647b3cdcSGeorge Liu std::vector<std::string> split; 3545647b3cdcSGeorge Liu boost::algorithm::split(split, postCodeID, boost::is_any_of("-")); 3546647b3cdcSGeorge Liu if (split.size() != 2 || split[0].length() < 2 || split[0].front() != 'B') 3547647b3cdcSGeorge Liu { 3548647b3cdcSGeorge Liu return false; 3549647b3cdcSGeorge Liu } 3550647b3cdcSGeorge Liu 3551ca45aa3cSEd Tanous // NOLINTNEXTLINE(cppcoreguidelines-pro-bounds-pointer-arithmetic) 3552647b3cdcSGeorge Liu const char* start = split[0].data() + 1; 3553ca45aa3cSEd Tanous // NOLINTNEXTLINE(cppcoreguidelines-pro-bounds-pointer-arithmetic) 3554647b3cdcSGeorge Liu const char* end = split[0].data() + split[0].size(); 3555647b3cdcSGeorge Liu auto [ptrIndex, ecIndex] = std::from_chars(start, end, index); 3556647b3cdcSGeorge Liu 3557647b3cdcSGeorge Liu if (ptrIndex != end || ecIndex != std::errc()) 3558647b3cdcSGeorge Liu { 3559647b3cdcSGeorge Liu return false; 3560647b3cdcSGeorge Liu } 3561647b3cdcSGeorge Liu 3562647b3cdcSGeorge Liu start = split[1].data(); 3563ca45aa3cSEd Tanous 3564ca45aa3cSEd Tanous // NOLINTNEXTLINE(cppcoreguidelines-pro-bounds-pointer-arithmetic) 3565647b3cdcSGeorge Liu end = split[1].data() + split[1].size(); 3566647b3cdcSGeorge Liu auto [ptrValue, ecValue] = std::from_chars(start, end, currentValue); 3567647b3cdcSGeorge Liu 3568517d9a58STony Lee return ptrValue == end && ecValue == std::errc(); 3569647b3cdcSGeorge Liu } 3570647b3cdcSGeorge Liu 3571647b3cdcSGeorge Liu inline void requestRoutesPostCodesEntryAdditionalData(App& app) 3572647b3cdcSGeorge Liu { 35730fda0f12SGeorge Liu BMCWEB_ROUTE( 35740fda0f12SGeorge Liu app, 35750fda0f12SGeorge Liu "/redfish/v1/Systems/system/LogServices/PostCodes/Entries/<str>/attachment/") 3576647b3cdcSGeorge Liu .privileges(redfish::privileges::getLogEntry) 3577647b3cdcSGeorge Liu .methods(boost::beast::http::verb::get)( 357845ca1b86SEd Tanous [&app](const crow::Request& req, 3579647b3cdcSGeorge Liu const std::shared_ptr<bmcweb::AsyncResp>& asyncResp, 3580647b3cdcSGeorge Liu const std::string& postCodeID) { 35813ba00073SCarson Labrado if (!redfish::setUpRedfishRoute(app, req, asyncResp)) 358245ca1b86SEd Tanous { 358345ca1b86SEd Tanous return; 358445ca1b86SEd Tanous } 3585002d39b4SEd Tanous if (!http_helpers::isOctetAccepted(req.getHeaderValue("Accept"))) 3586647b3cdcSGeorge Liu { 3587002d39b4SEd Tanous asyncResp->res.result(boost::beast::http::status::bad_request); 3588647b3cdcSGeorge Liu return; 3589647b3cdcSGeorge Liu } 3590647b3cdcSGeorge Liu 3591647b3cdcSGeorge Liu uint64_t currentValue = 0; 3592647b3cdcSGeorge Liu uint16_t index = 0; 3593647b3cdcSGeorge Liu if (!parsePostCode(postCodeID, currentValue, index)) 3594647b3cdcSGeorge Liu { 3595002d39b4SEd Tanous messages::resourceNotFound(asyncResp->res, "LogEntry", postCodeID); 3596647b3cdcSGeorge Liu return; 3597647b3cdcSGeorge Liu } 3598647b3cdcSGeorge Liu 3599647b3cdcSGeorge Liu crow::connections::systemBus->async_method_call( 3600647b3cdcSGeorge Liu [asyncResp, postCodeID, currentValue]( 3601647b3cdcSGeorge Liu const boost::system::error_code ec, 3602002d39b4SEd Tanous const std::vector<std::tuple<uint64_t, std::vector<uint8_t>>>& 3603002d39b4SEd Tanous postcodes) { 3604647b3cdcSGeorge Liu if (ec.value() == EBADR) 3605647b3cdcSGeorge Liu { 3606002d39b4SEd Tanous messages::resourceNotFound(asyncResp->res, "LogEntry", 3607002d39b4SEd Tanous postCodeID); 3608647b3cdcSGeorge Liu return; 3609647b3cdcSGeorge Liu } 3610647b3cdcSGeorge Liu if (ec) 3611647b3cdcSGeorge Liu { 3612647b3cdcSGeorge Liu BMCWEB_LOG_DEBUG << "DBUS response error " << ec; 3613647b3cdcSGeorge Liu messages::internalError(asyncResp->res); 3614647b3cdcSGeorge Liu return; 3615647b3cdcSGeorge Liu } 3616647b3cdcSGeorge Liu 3617647b3cdcSGeorge Liu size_t value = static_cast<size_t>(currentValue) - 1; 3618002d39b4SEd Tanous if (value == std::string::npos || postcodes.size() < currentValue) 3619647b3cdcSGeorge Liu { 3620647b3cdcSGeorge Liu BMCWEB_LOG_ERROR << "Wrong currentValue value"; 3621002d39b4SEd Tanous messages::resourceNotFound(asyncResp->res, "LogEntry", 3622002d39b4SEd Tanous postCodeID); 3623647b3cdcSGeorge Liu return; 3624647b3cdcSGeorge Liu } 3625647b3cdcSGeorge Liu 36269eb808c1SEd Tanous const auto& [tID, c] = postcodes[value]; 362746ff87baSEd Tanous if (c.empty()) 3628647b3cdcSGeorge Liu { 3629647b3cdcSGeorge Liu BMCWEB_LOG_INFO << "No found post code data"; 3630002d39b4SEd Tanous messages::resourceNotFound(asyncResp->res, "LogEntry", 3631002d39b4SEd Tanous postCodeID); 3632647b3cdcSGeorge Liu return; 3633647b3cdcSGeorge Liu } 363446ff87baSEd Tanous // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast) 363546ff87baSEd Tanous const char* d = reinterpret_cast<const char*>(c.data()); 363646ff87baSEd Tanous std::string_view strData(d, c.size()); 3637647b3cdcSGeorge Liu 3638d9f6c621SEd Tanous asyncResp->res.addHeader(boost::beast::http::field::content_type, 3639647b3cdcSGeorge Liu "application/octet-stream"); 3640d9f6c621SEd Tanous asyncResp->res.addHeader( 3641d9f6c621SEd Tanous boost::beast::http::field::content_transfer_encoding, "Base64"); 3642002d39b4SEd Tanous asyncResp->res.body() = crow::utility::base64encode(strData); 3643647b3cdcSGeorge Liu }, 3644647b3cdcSGeorge Liu "xyz.openbmc_project.State.Boot.PostCode0", 3645647b3cdcSGeorge Liu "/xyz/openbmc_project/State/Boot/PostCode0", 3646002d39b4SEd Tanous "xyz.openbmc_project.State.Boot.PostCode", "GetPostCodes", index); 3647647b3cdcSGeorge Liu }); 3648647b3cdcSGeorge Liu } 3649647b3cdcSGeorge Liu 36507e860f15SJohn Edward Broadbent inline void requestRoutesPostCodesEntry(App& app) 3651a3316fc6SZhikuiRen { 36527e860f15SJohn Edward Broadbent BMCWEB_ROUTE( 36537e860f15SJohn Edward Broadbent app, "/redfish/v1/Systems/system/LogServices/PostCodes/Entries/<str>/") 3654ed398213SEd Tanous .privileges(redfish::privileges::getLogEntry) 36557e860f15SJohn Edward Broadbent .methods(boost::beast::http::verb::get)( 365645ca1b86SEd Tanous [&app](const crow::Request& req, 36577e860f15SJohn Edward Broadbent const std::shared_ptr<bmcweb::AsyncResp>& asyncResp, 36587e860f15SJohn Edward Broadbent const std::string& targetID) { 36593ba00073SCarson Labrado if (!redfish::setUpRedfishRoute(app, req, asyncResp)) 366045ca1b86SEd Tanous { 366145ca1b86SEd Tanous return; 366245ca1b86SEd Tanous } 3663647b3cdcSGeorge Liu uint16_t bootIndex = 0; 3664647b3cdcSGeorge Liu uint64_t codeIndex = 0; 3665647b3cdcSGeorge Liu if (!parsePostCode(targetID, codeIndex, bootIndex)) 3666a3316fc6SZhikuiRen { 3667a3316fc6SZhikuiRen // Requested ID was not found 3668ace85d60SEd Tanous messages::resourceMissingAtURI(asyncResp->res, req.urlView); 3669a3316fc6SZhikuiRen return; 3670a3316fc6SZhikuiRen } 3671a3316fc6SZhikuiRen if (bootIndex == 0 || codeIndex == 0) 3672a3316fc6SZhikuiRen { 3673a3316fc6SZhikuiRen BMCWEB_LOG_DEBUG << "Get Post Code invalid entry string " 36747e860f15SJohn Edward Broadbent << targetID; 3675a3316fc6SZhikuiRen } 3676a3316fc6SZhikuiRen 3677002d39b4SEd Tanous asyncResp->res.jsonValue["@odata.type"] = "#LogEntry.v1_4_0.LogEntry"; 3678a3316fc6SZhikuiRen asyncResp->res.jsonValue["@odata.id"] = 36790fda0f12SGeorge Liu "/redfish/v1/Systems/system/LogServices/PostCodes/Entries"; 3680a3316fc6SZhikuiRen asyncResp->res.jsonValue["Name"] = "BIOS POST Code Log Entries"; 3681a3316fc6SZhikuiRen asyncResp->res.jsonValue["Description"] = 3682a3316fc6SZhikuiRen "Collection of POST Code Log Entries"; 3683a3316fc6SZhikuiRen asyncResp->res.jsonValue["Members"] = nlohmann::json::array(); 3684a3316fc6SZhikuiRen asyncResp->res.jsonValue["Members@odata.count"] = 0; 3685a3316fc6SZhikuiRen 3686a3316fc6SZhikuiRen getPostCodeForEntry(asyncResp, bootIndex, codeIndex); 36877e860f15SJohn Edward Broadbent }); 3688a3316fc6SZhikuiRen } 3689a3316fc6SZhikuiRen 36901da66f75SEd Tanous } // namespace redfish 3691