xref: /openbmc/bmcweb/redfish-core/lib/log_services.hpp (revision bb759e3aeaadfec9f3aac4485f253bcc8a523e4c)
1 /*
2 // Copyright (c) 2018 Intel Corporation
3 //
4 // Licensed under the Apache License, Version 2.0 (the "License");
5 // you may not use this file except in compliance with the License.
6 // You may obtain a copy of the License at
7 //
8 //      http://www.apache.org/licenses/LICENSE-2.0
9 //
10 // Unless required by applicable law or agreed to in writing, software
11 // distributed under the License is distributed on an "AS IS" BASIS,
12 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 // See the License for the specific language governing permissions and
14 // limitations under the License.
15 */
16 #pragma once
17 
18 #include "gzfile.hpp"
19 #include "http_utility.hpp"
20 #include "human_sort.hpp"
21 #include "registries.hpp"
22 #include "registries/base_message_registry.hpp"
23 #include "registries/openbmc_message_registry.hpp"
24 #include "task.hpp"
25 
26 #include <systemd/sd-journal.h>
27 #include <unistd.h>
28 
29 #include <app.hpp>
30 #include <boost/algorithm/string/case_conv.hpp>
31 #include <boost/algorithm/string/classification.hpp>
32 #include <boost/algorithm/string/replace.hpp>
33 #include <boost/algorithm/string/split.hpp>
34 #include <boost/beast/http/verb.hpp>
35 #include <boost/container/flat_map.hpp>
36 #include <boost/system/linux_error.hpp>
37 #include <dbus_utility.hpp>
38 #include <error_messages.hpp>
39 #include <query.hpp>
40 #include <registries/privilege_registry.hpp>
41 #include <sdbusplus/asio/property.hpp>
42 #include <sdbusplus/unpack_properties.hpp>
43 #include <utils/dbus_utils.hpp>
44 #include <utils/time_utils.hpp>
45 
46 #include <charconv>
47 #include <filesystem>
48 #include <optional>
49 #include <span>
50 #include <string_view>
51 #include <variant>
52 
53 namespace redfish
54 {
55 
56 constexpr char const* crashdumpObject = "com.intel.crashdump";
57 constexpr char const* crashdumpPath = "/com/intel/crashdump";
58 constexpr char const* crashdumpInterface = "com.intel.crashdump";
59 constexpr char const* deleteAllInterface =
60     "xyz.openbmc_project.Collection.DeleteAll";
61 constexpr char const* crashdumpOnDemandInterface =
62     "com.intel.crashdump.OnDemand";
63 constexpr char const* crashdumpTelemetryInterface =
64     "com.intel.crashdump.Telemetry";
65 
66 namespace registries
67 {
68 static const Message*
69     getMessageFromRegistry(const std::string& messageKey,
70                            const std::span<const MessageEntry> registry)
71 {
72     std::span<const MessageEntry>::iterator messageIt =
73         std::find_if(registry.begin(), registry.end(),
74                      [&messageKey](const MessageEntry& messageEntry) {
75         return std::strcmp(messageEntry.first, messageKey.c_str()) == 0;
76         });
77     if (messageIt != registry.end())
78     {
79         return &messageIt->second;
80     }
81 
82     return nullptr;
83 }
84 
85 static const Message* getMessage(const std::string_view& messageID)
86 {
87     // Redfish MessageIds are in the form
88     // RegistryName.MajorVersion.MinorVersion.MessageKey, so parse it to find
89     // the right Message
90     std::vector<std::string> fields;
91     fields.reserve(4);
92     boost::split(fields, messageID, boost::is_any_of("."));
93     const std::string& registryName = fields[0];
94     const std::string& messageKey = fields[3];
95 
96     // Find the right registry and check it for the MessageKey
97     if (std::string(base::header.registryPrefix) == registryName)
98     {
99         return getMessageFromRegistry(
100             messageKey, std::span<const MessageEntry>(base::registry));
101     }
102     if (std::string(openbmc::header.registryPrefix) == registryName)
103     {
104         return getMessageFromRegistry(
105             messageKey, std::span<const MessageEntry>(openbmc::registry));
106     }
107     return nullptr;
108 }
109 } // namespace registries
110 
111 namespace fs = std::filesystem;
112 
113 inline std::string translateSeverityDbusToRedfish(const std::string& s)
114 {
115     if ((s == "xyz.openbmc_project.Logging.Entry.Level.Alert") ||
116         (s == "xyz.openbmc_project.Logging.Entry.Level.Critical") ||
117         (s == "xyz.openbmc_project.Logging.Entry.Level.Emergency") ||
118         (s == "xyz.openbmc_project.Logging.Entry.Level.Error"))
119     {
120         return "Critical";
121     }
122     if ((s == "xyz.openbmc_project.Logging.Entry.Level.Debug") ||
123         (s == "xyz.openbmc_project.Logging.Entry.Level.Informational") ||
124         (s == "xyz.openbmc_project.Logging.Entry.Level.Notice"))
125     {
126         return "OK";
127     }
128     if (s == "xyz.openbmc_project.Logging.Entry.Level.Warning")
129     {
130         return "Warning";
131     }
132     return "";
133 }
134 
135 inline static int getJournalMetadata(sd_journal* journal,
136                                      const std::string_view& field,
137                                      std::string_view& contents)
138 {
139     const char* data = nullptr;
140     size_t length = 0;
141     int ret = 0;
142     // Get the metadata from the requested field of the journal entry
143     // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
144     const void** dataVoid = reinterpret_cast<const void**>(&data);
145 
146     ret = sd_journal_get_data(journal, field.data(), dataVoid, &length);
147     if (ret < 0)
148     {
149         return ret;
150     }
151     contents = std::string_view(data, length);
152     // Only use the content after the "=" character.
153     contents.remove_prefix(std::min(contents.find('=') + 1, contents.size()));
154     return ret;
155 }
156 
157 inline static int getJournalMetadata(sd_journal* journal,
158                                      const std::string_view& field,
159                                      const int& base, long int& contents)
160 {
161     int ret = 0;
162     std::string_view metadata;
163     // Get the metadata from the requested field of the journal entry
164     ret = getJournalMetadata(journal, field, metadata);
165     if (ret < 0)
166     {
167         return ret;
168     }
169     contents = strtol(metadata.data(), nullptr, base);
170     return ret;
171 }
172 
173 inline static bool getEntryTimestamp(sd_journal* journal,
174                                      std::string& entryTimestamp)
175 {
176     int ret = 0;
177     uint64_t timestamp = 0;
178     ret = sd_journal_get_realtime_usec(journal, &timestamp);
179     if (ret < 0)
180     {
181         BMCWEB_LOG_ERROR << "Failed to read entry timestamp: "
182                          << strerror(-ret);
183         return false;
184     }
185     entryTimestamp =
186         redfish::time_utils::getDateTimeUint(timestamp / 1000 / 1000);
187     return true;
188 }
189 
190 inline static bool getUniqueEntryID(sd_journal* journal, std::string& entryID,
191                                     const bool firstEntry = true)
192 {
193     int ret = 0;
194     static uint64_t prevTs = 0;
195     static int index = 0;
196     if (firstEntry)
197     {
198         prevTs = 0;
199     }
200 
201     // Get the entry timestamp
202     uint64_t curTs = 0;
203     ret = sd_journal_get_realtime_usec(journal, &curTs);
204     if (ret < 0)
205     {
206         BMCWEB_LOG_ERROR << "Failed to read entry timestamp: "
207                          << strerror(-ret);
208         return false;
209     }
210     // If the timestamp isn't unique, increment the index
211     if (curTs == prevTs)
212     {
213         index++;
214     }
215     else
216     {
217         // Otherwise, reset it
218         index = 0;
219     }
220     // Save the timestamp
221     prevTs = curTs;
222 
223     entryID = std::to_string(curTs);
224     if (index > 0)
225     {
226         entryID += "_" + std::to_string(index);
227     }
228     return true;
229 }
230 
231 static bool getUniqueEntryID(const std::string& logEntry, std::string& entryID,
232                              const bool firstEntry = true)
233 {
234     static time_t prevTs = 0;
235     static int index = 0;
236     if (firstEntry)
237     {
238         prevTs = 0;
239     }
240 
241     // Get the entry timestamp
242     std::time_t curTs = 0;
243     std::tm timeStruct = {};
244     std::istringstream entryStream(logEntry);
245     if (entryStream >> std::get_time(&timeStruct, "%Y-%m-%dT%H:%M:%S"))
246     {
247         curTs = std::mktime(&timeStruct);
248     }
249     // If the timestamp isn't unique, increment the index
250     if (curTs == prevTs)
251     {
252         index++;
253     }
254     else
255     {
256         // Otherwise, reset it
257         index = 0;
258     }
259     // Save the timestamp
260     prevTs = curTs;
261 
262     entryID = std::to_string(curTs);
263     if (index > 0)
264     {
265         entryID += "_" + std::to_string(index);
266     }
267     return true;
268 }
269 
270 inline static bool
271     getTimestampFromID(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
272                        const std::string& entryID, uint64_t& timestamp,
273                        uint64_t& index)
274 {
275     if (entryID.empty())
276     {
277         return false;
278     }
279     // Convert the unique ID back to a timestamp to find the entry
280     std::string_view tsStr(entryID);
281 
282     auto underscorePos = tsStr.find('_');
283     if (underscorePos != std::string_view::npos)
284     {
285         // Timestamp has an index
286         tsStr.remove_suffix(tsStr.size() - underscorePos);
287         std::string_view indexStr(entryID);
288         indexStr.remove_prefix(underscorePos + 1);
289         auto [ptr, ec] = std::from_chars(
290             indexStr.data(), indexStr.data() + indexStr.size(), index);
291         if (ec != std::errc())
292         {
293             messages::resourceMissingAtURI(
294                 asyncResp->res, crow::utility::urlFromPieces(entryID));
295             return false;
296         }
297     }
298     // Timestamp has no index
299     auto [ptr, ec] =
300         std::from_chars(tsStr.data(), tsStr.data() + tsStr.size(), timestamp);
301     if (ec != std::errc())
302     {
303         messages::resourceMissingAtURI(asyncResp->res,
304                                        crow::utility::urlFromPieces(entryID));
305         return false;
306     }
307     return true;
308 }
309 
310 static bool
311     getRedfishLogFiles(std::vector<std::filesystem::path>& redfishLogFiles)
312 {
313     static const std::filesystem::path redfishLogDir = "/var/log";
314     static const std::string redfishLogFilename = "redfish";
315 
316     // Loop through the directory looking for redfish log files
317     for (const std::filesystem::directory_entry& dirEnt :
318          std::filesystem::directory_iterator(redfishLogDir))
319     {
320         // If we find a redfish log file, save the path
321         std::string filename = dirEnt.path().filename();
322         if (filename.starts_with(redfishLogFilename))
323         {
324             redfishLogFiles.emplace_back(redfishLogDir / filename);
325         }
326     }
327     // As the log files rotate, they are appended with a ".#" that is higher for
328     // the older logs. Since we don't expect more than 10 log files, we
329     // can just sort the list to get them in order from newest to oldest
330     std::sort(redfishLogFiles.begin(), redfishLogFiles.end());
331 
332     return !redfishLogFiles.empty();
333 }
334 
335 inline void parseDumpEntryFromDbusObject(
336     const dbus::utility::ManagedObjectType::value_type& object,
337     std::string& dumpStatus, uint64_t& size, uint64_t& timestampUs,
338     const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
339 {
340     for (const auto& interfaceMap : object.second)
341     {
342         if (interfaceMap.first == "xyz.openbmc_project.Common.Progress")
343         {
344             for (const auto& propertyMap : interfaceMap.second)
345             {
346                 if (propertyMap.first == "Status")
347                 {
348                     const auto* status =
349                         std::get_if<std::string>(&propertyMap.second);
350                     if (status == nullptr)
351                     {
352                         messages::internalError(asyncResp->res);
353                         break;
354                     }
355                     dumpStatus = *status;
356                 }
357             }
358         }
359         else if (interfaceMap.first == "xyz.openbmc_project.Dump.Entry")
360         {
361             for (const auto& propertyMap : interfaceMap.second)
362             {
363                 if (propertyMap.first == "Size")
364                 {
365                     const auto* sizePtr =
366                         std::get_if<uint64_t>(&propertyMap.second);
367                     if (sizePtr == nullptr)
368                     {
369                         messages::internalError(asyncResp->res);
370                         break;
371                     }
372                     size = *sizePtr;
373                     break;
374                 }
375             }
376         }
377         else if (interfaceMap.first == "xyz.openbmc_project.Time.EpochTime")
378         {
379             for (const auto& propertyMap : interfaceMap.second)
380             {
381                 if (propertyMap.first == "Elapsed")
382                 {
383                     const uint64_t* usecsTimeStamp =
384                         std::get_if<uint64_t>(&propertyMap.second);
385                     if (usecsTimeStamp == nullptr)
386                     {
387                         messages::internalError(asyncResp->res);
388                         break;
389                     }
390                     timestampUs = *usecsTimeStamp;
391                     break;
392                 }
393             }
394         }
395     }
396 }
397 
398 static std::string getDumpEntriesPath(const std::string& dumpType)
399 {
400     std::string entriesPath;
401 
402     if (dumpType == "BMC")
403     {
404         entriesPath = "/redfish/v1/Managers/bmc/LogServices/Dump/Entries/";
405     }
406     else if (dumpType == "FaultLog")
407     {
408         entriesPath = "/redfish/v1/Managers/bmc/LogServices/FaultLog/Entries/";
409     }
410     else if (dumpType == "System")
411     {
412         entriesPath = "/redfish/v1/Systems/system/LogServices/Dump/Entries/";
413     }
414     else
415     {
416         BMCWEB_LOG_ERROR << "getDumpEntriesPath() invalid dump type: "
417                          << dumpType;
418     }
419 
420     // Returns empty string on error
421     return entriesPath;
422 }
423 
424 inline void
425     getDumpEntryCollection(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
426                            const std::string& dumpType)
427 {
428     std::string entriesPath = getDumpEntriesPath(dumpType);
429     if (entriesPath.empty())
430     {
431         messages::internalError(asyncResp->res);
432         return;
433     }
434 
435     crow::connections::systemBus->async_method_call(
436         [asyncResp, entriesPath,
437          dumpType](const boost::system::error_code ec,
438                    dbus::utility::ManagedObjectType& resp) {
439         if (ec)
440         {
441             BMCWEB_LOG_ERROR << "DumpEntry resp_handler got error " << ec;
442             messages::internalError(asyncResp->res);
443             return;
444         }
445 
446         // Remove ending slash
447         std::string odataIdStr = entriesPath;
448         if (!odataIdStr.empty())
449         {
450             odataIdStr.pop_back();
451         }
452 
453         asyncResp->res.jsonValue["@odata.type"] =
454             "#LogEntryCollection.LogEntryCollection";
455         asyncResp->res.jsonValue["@odata.id"] = std::move(odataIdStr);
456         asyncResp->res.jsonValue["Name"] = dumpType + " Dump Entries";
457         asyncResp->res.jsonValue["Description"] =
458             "Collection of " + dumpType + " Dump Entries";
459 
460         nlohmann::json& entriesArray = asyncResp->res.jsonValue["Members"];
461         entriesArray = nlohmann::json::array();
462         std::string dumpEntryPath =
463             "/xyz/openbmc_project/dump/" +
464             std::string(boost::algorithm::to_lower_copy(dumpType)) + "/entry/";
465 
466         std::sort(resp.begin(), resp.end(), [](const auto& l, const auto& r) {
467             return AlphanumLess<std::string>()(l.first.filename(),
468                                                r.first.filename());
469         });
470 
471         for (auto& object : resp)
472         {
473             if (object.first.str.find(dumpEntryPath) == std::string::npos)
474             {
475                 continue;
476             }
477             uint64_t timestampUs = 0;
478             uint64_t size = 0;
479             std::string dumpStatus;
480             nlohmann::json::object_t thisEntry;
481 
482             std::string entryID = object.first.filename();
483             if (entryID.empty())
484             {
485                 continue;
486             }
487 
488             parseDumpEntryFromDbusObject(object, dumpStatus, size, timestampUs,
489                                          asyncResp);
490 
491             if (dumpStatus !=
492                     "xyz.openbmc_project.Common.Progress.OperationStatus.Completed" &&
493                 !dumpStatus.empty())
494             {
495                 // Dump status is not Complete, no need to enumerate
496                 continue;
497             }
498 
499             thisEntry["@odata.type"] = "#LogEntry.v1_9_0.LogEntry";
500             thisEntry["@odata.id"] = entriesPath + entryID;
501             thisEntry["Id"] = entryID;
502             thisEntry["EntryType"] = "Event";
503             thisEntry["Name"] = dumpType + " Dump Entry";
504 
505             if (dumpType == "BMC")
506             {
507                 thisEntry["Created"] = redfish::time_utils::getDateTimeUint(
508                     timestampUs / 1000 / 1000);
509                 thisEntry["DiagnosticDataType"] = "Manager";
510                 thisEntry["AdditionalDataURI"] =
511                     entriesPath + entryID + "/attachment";
512                 thisEntry["AdditionalDataSizeBytes"] = size;
513             }
514             else if (dumpType == "FaultLog")
515             {
516                 thisEntry["Created"] =
517                     redfish::time_utils::getDateTimeUintUs(timestampUs);
518             }
519             else if (dumpType == "System")
520             {
521                 thisEntry["Created"] = redfish::time_utils::getDateTimeUint(
522                     timestampUs / 1000 / 1000);
523                 thisEntry["DiagnosticDataType"] = "OEM";
524                 thisEntry["OEMDiagnosticDataType"] = "System";
525                 thisEntry["AdditionalDataURI"] =
526                     entriesPath + entryID + "/attachment";
527                 thisEntry["AdditionalDataSizeBytes"] = size;
528             }
529             entriesArray.push_back(std::move(thisEntry));
530         }
531         asyncResp->res.jsonValue["Members@odata.count"] = entriesArray.size();
532         },
533         "xyz.openbmc_project.Dump.Manager", "/xyz/openbmc_project/dump",
534         "org.freedesktop.DBus.ObjectManager", "GetManagedObjects");
535 }
536 
537 inline void
538     getDumpEntryById(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
539                      const std::string& entryID, const std::string& dumpType)
540 {
541     std::string entriesPath = getDumpEntriesPath(dumpType);
542     if (entriesPath.empty())
543     {
544         messages::internalError(asyncResp->res);
545         return;
546     }
547 
548     crow::connections::systemBus->async_method_call(
549         [asyncResp, entryID, dumpType,
550          entriesPath](const boost::system::error_code ec,
551                       const dbus::utility::ManagedObjectType& resp) {
552         if (ec)
553         {
554             BMCWEB_LOG_ERROR << "DumpEntry resp_handler got error " << ec;
555             messages::internalError(asyncResp->res);
556             return;
557         }
558 
559         bool foundDumpEntry = false;
560         std::string dumpEntryPath =
561             "/xyz/openbmc_project/dump/" +
562             std::string(boost::algorithm::to_lower_copy(dumpType)) + "/entry/";
563 
564         for (const auto& objectPath : resp)
565         {
566             if (objectPath.first.str != dumpEntryPath + entryID)
567             {
568                 continue;
569             }
570 
571             foundDumpEntry = true;
572             uint64_t timestampUs = 0;
573             uint64_t size = 0;
574             std::string dumpStatus;
575 
576             parseDumpEntryFromDbusObject(objectPath, dumpStatus, size,
577                                          timestampUs, asyncResp);
578 
579             if (dumpStatus !=
580                     "xyz.openbmc_project.Common.Progress.OperationStatus.Completed" &&
581                 !dumpStatus.empty())
582             {
583                 // Dump status is not Complete
584                 // return not found until status is changed to Completed
585                 messages::resourceNotFound(asyncResp->res, dumpType + " dump",
586                                            entryID);
587                 return;
588             }
589 
590             asyncResp->res.jsonValue["@odata.type"] =
591                 "#LogEntry.v1_9_0.LogEntry";
592             asyncResp->res.jsonValue["@odata.id"] = entriesPath + entryID;
593             asyncResp->res.jsonValue["Id"] = entryID;
594             asyncResp->res.jsonValue["EntryType"] = "Event";
595             asyncResp->res.jsonValue["Name"] = dumpType + " Dump Entry";
596 
597             if (dumpType == "BMC")
598             {
599                 asyncResp->res.jsonValue["Created"] =
600                     redfish::time_utils::getDateTimeUint(timestampUs / 1000 /
601                                                          1000);
602                 asyncResp->res.jsonValue["DiagnosticDataType"] = "Manager";
603                 asyncResp->res.jsonValue["AdditionalDataURI"] =
604                     entriesPath + entryID + "/attachment";
605                 asyncResp->res.jsonValue["AdditionalDataSizeBytes"] = size;
606             }
607             else if (dumpType == "FaultLog")
608             {
609                 asyncResp->res.jsonValue["Created"] =
610                     redfish::time_utils::getDateTimeUintUs(timestampUs);
611             }
612             else if (dumpType == "System")
613             {
614                 asyncResp->res.jsonValue["Created"] =
615                     redfish::time_utils::getDateTimeUint(timestampUs / 1000 /
616                                                          1000);
617                 asyncResp->res.jsonValue["DiagnosticDataType"] = "OEM";
618                 asyncResp->res.jsonValue["OEMDiagnosticDataType"] = "System";
619                 asyncResp->res.jsonValue["AdditionalDataURI"] =
620                     entriesPath + entryID + "/attachment";
621                 asyncResp->res.jsonValue["AdditionalDataSizeBytes"] = size;
622             }
623         }
624         if (!foundDumpEntry)
625         {
626             BMCWEB_LOG_ERROR << "Can't find Dump Entry";
627             messages::internalError(asyncResp->res);
628             return;
629         }
630         },
631         "xyz.openbmc_project.Dump.Manager", "/xyz/openbmc_project/dump",
632         "org.freedesktop.DBus.ObjectManager", "GetManagedObjects");
633 }
634 
635 inline void deleteDumpEntry(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
636                             const std::string& entryID,
637                             const std::string& dumpType)
638 {
639     auto respHandler =
640         [asyncResp, entryID](const boost::system::error_code ec) {
641         BMCWEB_LOG_DEBUG << "Dump Entry doDelete callback: Done";
642         if (ec)
643         {
644             if (ec.value() == EBADR)
645             {
646                 messages::resourceNotFound(asyncResp->res, "LogEntry", entryID);
647                 return;
648             }
649             BMCWEB_LOG_ERROR << "Dump (DBus) doDelete respHandler got error "
650                              << ec << " entryID=" << entryID;
651             messages::internalError(asyncResp->res);
652             return;
653         }
654     };
655     crow::connections::systemBus->async_method_call(
656         respHandler, "xyz.openbmc_project.Dump.Manager",
657         "/xyz/openbmc_project/dump/" +
658             std::string(boost::algorithm::to_lower_copy(dumpType)) + "/entry/" +
659             entryID,
660         "xyz.openbmc_project.Object.Delete", "Delete");
661 }
662 
663 inline void
664     createDumpTaskCallback(task::Payload&& payload,
665                            const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
666                            const uint32_t& dumpId, const std::string& dumpPath,
667                            const std::string& dumpType)
668 {
669     std::shared_ptr<task::TaskData> task = task::TaskData::createTask(
670         [dumpId, dumpPath,
671          dumpType](boost::system::error_code err, sdbusplus::message_t& m,
672                    const std::shared_ptr<task::TaskData>& taskData) {
673         if (err)
674         {
675             BMCWEB_LOG_ERROR << "Error in creating a dump";
676             taskData->state = "Cancelled";
677             return task::completed;
678         }
679 
680         dbus::utility::DBusInteracesMap interfacesList;
681 
682         sdbusplus::message::object_path objPath;
683 
684         m.read(objPath, interfacesList);
685 
686         if (objPath.str ==
687             "/xyz/openbmc_project/dump/" +
688                 std::string(boost::algorithm::to_lower_copy(dumpType)) +
689                 "/entry/" + std::to_string(dumpId))
690         {
691             nlohmann::json retMessage = messages::success();
692             taskData->messages.emplace_back(retMessage);
693 
694             std::string headerLoc =
695                 "Location: " + dumpPath + std::to_string(dumpId);
696             taskData->payload->httpHeaders.emplace_back(std::move(headerLoc));
697 
698             taskData->state = "Completed";
699             return task::completed;
700         }
701         return task::completed;
702         },
703         "type='signal',interface='org.freedesktop.DBus.ObjectManager',"
704         "member='InterfacesAdded', "
705         "path='/xyz/openbmc_project/dump'");
706 
707     task->startTimer(std::chrono::minutes(3));
708     task->populateResp(asyncResp->res);
709     task->payload.emplace(std::move(payload));
710 }
711 
712 inline void createDump(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
713                        const crow::Request& req, const std::string& dumpType)
714 {
715     std::string dumpPath = getDumpEntriesPath(dumpType);
716     if (dumpPath.empty())
717     {
718         messages::internalError(asyncResp->res);
719         return;
720     }
721 
722     std::optional<std::string> diagnosticDataType;
723     std::optional<std::string> oemDiagnosticDataType;
724 
725     if (!redfish::json_util::readJsonAction(
726             req, asyncResp->res, "DiagnosticDataType", diagnosticDataType,
727             "OEMDiagnosticDataType", oemDiagnosticDataType))
728     {
729         return;
730     }
731 
732     if (dumpType == "System")
733     {
734         if (!oemDiagnosticDataType || !diagnosticDataType)
735         {
736             BMCWEB_LOG_ERROR
737                 << "CreateDump action parameter 'DiagnosticDataType'/'OEMDiagnosticDataType' value not found!";
738             messages::actionParameterMissing(
739                 asyncResp->res, "CollectDiagnosticData",
740                 "DiagnosticDataType & OEMDiagnosticDataType");
741             return;
742         }
743         if ((*oemDiagnosticDataType != "System") ||
744             (*diagnosticDataType != "OEM"))
745         {
746             BMCWEB_LOG_ERROR << "Wrong parameter values passed";
747             messages::internalError(asyncResp->res);
748             return;
749         }
750         dumpPath = "/redfish/v1/Systems/system/LogServices/Dump/";
751     }
752     else if (dumpType == "BMC")
753     {
754         if (!diagnosticDataType)
755         {
756             BMCWEB_LOG_ERROR
757                 << "CreateDump action parameter 'DiagnosticDataType' not found!";
758             messages::actionParameterMissing(
759                 asyncResp->res, "CollectDiagnosticData", "DiagnosticDataType");
760             return;
761         }
762         if (*diagnosticDataType != "Manager")
763         {
764             BMCWEB_LOG_ERROR
765                 << "Wrong parameter value passed for 'DiagnosticDataType'";
766             messages::internalError(asyncResp->res);
767             return;
768         }
769         dumpPath = "/redfish/v1/Managers/bmc/LogServices/Dump/";
770     }
771     else
772     {
773         BMCWEB_LOG_ERROR << "CreateDump failed. Unknown dump type";
774         messages::internalError(asyncResp->res);
775         return;
776     }
777 
778     crow::connections::systemBus->async_method_call(
779         [asyncResp, payload(task::Payload(req)), dumpPath,
780          dumpType](const boost::system::error_code ec,
781                    const sdbusplus::message::message& msg,
782                    const uint32_t& dumpId) mutable {
783         if (ec)
784         {
785             BMCWEB_LOG_ERROR << "CreateDump resp_handler got error " << ec;
786             const sd_bus_error* dbusError = msg.get_error();
787             if (dbusError == nullptr)
788             {
789                 messages::internalError(asyncResp->res);
790                 return;
791             }
792 
793             BMCWEB_LOG_ERROR << "CreateDump DBus error: " << dbusError->name
794                              << " and error msg: " << dbusError->message;
795             if (std::string_view(
796                     "xyz.openbmc_project.Common.Error.NotAllowed") ==
797                 dbusError->name)
798             {
799                 messages::resourceInStandby(asyncResp->res);
800                 return;
801             }
802             if (std::string_view(
803                     "xyz.openbmc_project.Dump.Create.Error.Disabled") ==
804                 dbusError->name)
805             {
806                 messages::serviceDisabled(asyncResp->res, dumpPath);
807                 return;
808             }
809             if (std::string_view(
810                     "xyz.openbmc_project.Common.Error.Unavailable") ==
811                 dbusError->name)
812             {
813                 messages::resourceInUse(asyncResp->res);
814                 return;
815             }
816             // Other Dbus errors such as:
817             // xyz.openbmc_project.Common.Error.InvalidArgument &
818             // org.freedesktop.DBus.Error.InvalidArgs are all related to
819             // the dbus call that is made here in the bmcweb
820             // implementation and has nothing to do with the client's
821             // input in the request. Hence, returning internal error
822             // back to the client.
823             messages::internalError(asyncResp->res);
824             return;
825         }
826         BMCWEB_LOG_DEBUG << "Dump Created. Id: " << dumpId;
827 
828         createDumpTaskCallback(std::move(payload), asyncResp, dumpId, dumpPath,
829                                dumpType);
830         },
831         "xyz.openbmc_project.Dump.Manager",
832         "/xyz/openbmc_project/dump/" +
833             std::string(boost::algorithm::to_lower_copy(dumpType)),
834         "xyz.openbmc_project.Dump.Create", "CreateDump");
835 }
836 
837 inline void clearDump(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
838                       const std::string& dumpType)
839 {
840     std::string dumpTypeLowerCopy =
841         std::string(boost::algorithm::to_lower_copy(dumpType));
842 
843     crow::connections::systemBus->async_method_call(
844         [asyncResp, dumpType](
845             const boost::system::error_code ec,
846             const dbus::utility::MapperGetSubTreePathsResponse& subTreePaths) {
847         if (ec)
848         {
849             BMCWEB_LOG_ERROR << "resp_handler got error " << ec;
850             messages::internalError(asyncResp->res);
851             return;
852         }
853 
854         for (const std::string& path : subTreePaths)
855         {
856             sdbusplus::message::object_path objPath(path);
857             std::string logID = objPath.filename();
858             if (logID.empty())
859             {
860                 continue;
861             }
862             deleteDumpEntry(asyncResp, logID, dumpType);
863         }
864         },
865         "xyz.openbmc_project.ObjectMapper",
866         "/xyz/openbmc_project/object_mapper",
867         "xyz.openbmc_project.ObjectMapper", "GetSubTreePaths",
868         "/xyz/openbmc_project/dump/" + dumpTypeLowerCopy, 0,
869         std::array<std::string, 1>{"xyz.openbmc_project.Dump.Entry." +
870                                    dumpType});
871 }
872 
873 inline static void
874     parseCrashdumpParameters(const dbus::utility::DBusPropertiesMap& params,
875                              std::string& filename, std::string& timestamp,
876                              std::string& logfile)
877 {
878     const std::string* filenamePtr = nullptr;
879     const std::string* timestampPtr = nullptr;
880     const std::string* logfilePtr = nullptr;
881 
882     const bool success = sdbusplus::unpackPropertiesNoThrow(
883         dbus_utils::UnpackErrorPrinter(), params, "Timestamp", timestampPtr,
884         "Filename", filenamePtr, "Log", logfilePtr);
885 
886     if (!success)
887     {
888         return;
889     }
890 
891     if (filenamePtr != nullptr)
892     {
893         filename = *filenamePtr;
894     }
895 
896     if (timestampPtr != nullptr)
897     {
898         timestamp = *timestampPtr;
899     }
900 
901     if (logfilePtr != nullptr)
902     {
903         logfile = *logfilePtr;
904     }
905 }
906 
907 constexpr char const* postCodeIface = "xyz.openbmc_project.State.Boot.PostCode";
908 inline void requestRoutesSystemLogServiceCollection(App& app)
909 {
910     /**
911      * Functions triggers appropriate requests on DBus
912      */
913     BMCWEB_ROUTE(app, "/redfish/v1/Systems/<str>/LogServices/")
914         .privileges(redfish::privileges::getLogServiceCollection)
915         .methods(boost::beast::http::verb::get)(
916             [&app](const crow::Request& req,
917                    const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
918                    const std::string& systemName) {
919         if (!redfish::setUpRedfishRoute(app, req, asyncResp))
920         {
921             return;
922         }
923         if (systemName != "system")
924         {
925             messages::resourceNotFound(asyncResp->res, "ComputerSystem",
926                                        systemName);
927             return;
928         }
929 
930         // Collections don't include the static data added by SubRoute
931         // because it has a duplicate entry for members
932         asyncResp->res.jsonValue["@odata.type"] =
933             "#LogServiceCollection.LogServiceCollection";
934         asyncResp->res.jsonValue["@odata.id"] =
935             "/redfish/v1/Systems/system/LogServices";
936         asyncResp->res.jsonValue["Name"] = "System Log Services Collection";
937         asyncResp->res.jsonValue["Description"] =
938             "Collection of LogServices for this Computer System";
939         nlohmann::json& logServiceArray = asyncResp->res.jsonValue["Members"];
940         logServiceArray = nlohmann::json::array();
941         nlohmann::json::object_t eventLog;
942         eventLog["@odata.id"] =
943             "/redfish/v1/Systems/system/LogServices/EventLog";
944         logServiceArray.push_back(std::move(eventLog));
945 #ifdef BMCWEB_ENABLE_REDFISH_DUMP_LOG
946         nlohmann::json::object_t dumpLog;
947         dumpLog["@odata.id"] = "/redfish/v1/Systems/system/LogServices/Dump";
948         logServiceArray.push_back(std::move(dumpLog));
949 #endif
950 
951 #ifdef BMCWEB_ENABLE_REDFISH_CPU_LOG
952         nlohmann::json::object_t crashdump;
953         crashdump["@odata.id"] =
954             "/redfish/v1/Systems/system/LogServices/Crashdump";
955         logServiceArray.push_back(std::move(crashdump));
956 #endif
957 
958 #ifdef BMCWEB_ENABLE_REDFISH_HOST_LOGGER
959         nlohmann::json::object_t hostlogger;
960         hostlogger["@odata.id"] =
961             "/redfish/v1/Systems/system/LogServices/HostLogger";
962         logServiceArray.push_back(std::move(hostlogger));
963 #endif
964         asyncResp->res.jsonValue["Members@odata.count"] =
965             logServiceArray.size();
966 
967         crow::connections::systemBus->async_method_call(
968             [asyncResp](const boost::system::error_code ec,
969                         const dbus::utility::MapperGetSubTreePathsResponse&
970                             subtreePath) {
971             if (ec)
972             {
973                 BMCWEB_LOG_ERROR << ec;
974                 return;
975             }
976 
977             for (const auto& pathStr : subtreePath)
978             {
979                 if (pathStr.find("PostCode") != std::string::npos)
980                 {
981                     nlohmann::json& logServiceArrayLocal =
982                         asyncResp->res.jsonValue["Members"];
983                     nlohmann::json::object_t member;
984                     member["@odata.id"] =
985                         "/redfish/v1/Systems/system/LogServices/PostCodes";
986 
987                     logServiceArrayLocal.push_back(std::move(member));
988 
989                     asyncResp->res.jsonValue["Members@odata.count"] =
990                         logServiceArrayLocal.size();
991                     return;
992                 }
993             }
994             },
995             "xyz.openbmc_project.ObjectMapper",
996             "/xyz/openbmc_project/object_mapper",
997             "xyz.openbmc_project.ObjectMapper", "GetSubTreePaths", "/", 0,
998             std::array<const char*, 1>{postCodeIface});
999         });
1000 }
1001 
1002 inline void requestRoutesEventLogService(App& app)
1003 {
1004     BMCWEB_ROUTE(app, "/redfish/v1/Systems/<str>/LogServices/EventLog/")
1005         .privileges(redfish::privileges::getLogService)
1006         .methods(boost::beast::http::verb::get)(
1007             [&app](const crow::Request& req,
1008                    const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
1009                    const std::string& systemName) {
1010         if (!redfish::setUpRedfishRoute(app, req, asyncResp))
1011         {
1012             return;
1013         }
1014         if (systemName != "system")
1015         {
1016             messages::resourceNotFound(asyncResp->res, "ComputerSystem",
1017                                        systemName);
1018             return;
1019         }
1020         asyncResp->res.jsonValue["@odata.id"] =
1021             "/redfish/v1/Systems/system/LogServices/EventLog";
1022         asyncResp->res.jsonValue["@odata.type"] =
1023             "#LogService.v1_1_0.LogService";
1024         asyncResp->res.jsonValue["Name"] = "Event Log Service";
1025         asyncResp->res.jsonValue["Description"] = "System Event Log Service";
1026         asyncResp->res.jsonValue["Id"] = "EventLog";
1027         asyncResp->res.jsonValue["OverWritePolicy"] = "WrapsWhenFull";
1028 
1029         std::pair<std::string, std::string> redfishDateTimeOffset =
1030             redfish::time_utils::getDateTimeOffsetNow();
1031 
1032         asyncResp->res.jsonValue["DateTime"] = redfishDateTimeOffset.first;
1033         asyncResp->res.jsonValue["DateTimeLocalOffset"] =
1034             redfishDateTimeOffset.second;
1035 
1036         asyncResp->res.jsonValue["Entries"]["@odata.id"] =
1037             "/redfish/v1/Systems/system/LogServices/EventLog/Entries";
1038         asyncResp->res.jsonValue["Actions"]["#LogService.ClearLog"] = {
1039 
1040             {"target",
1041              "/redfish/v1/Systems/system/LogServices/EventLog/Actions/LogService.ClearLog"}};
1042         });
1043 }
1044 
1045 inline void requestRoutesJournalEventLogClear(App& app)
1046 {
1047     BMCWEB_ROUTE(
1048         app,
1049         "/redfish/v1/Systems/<str>/LogServices/EventLog/Actions/LogService.ClearLog/")
1050         .privileges({{"ConfigureComponents"}})
1051         .methods(boost::beast::http::verb::post)(
1052             [&app](const crow::Request& req,
1053                    const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
1054                    const std::string& systemName) {
1055         if (!redfish::setUpRedfishRoute(app, req, asyncResp))
1056         {
1057             return;
1058         }
1059         if (systemName != "system")
1060         {
1061             messages::resourceNotFound(asyncResp->res, "ComputerSystem",
1062                                        systemName);
1063             return;
1064         }
1065         // Clear the EventLog by deleting the log files
1066         std::vector<std::filesystem::path> redfishLogFiles;
1067         if (getRedfishLogFiles(redfishLogFiles))
1068         {
1069             for (const std::filesystem::path& file : redfishLogFiles)
1070             {
1071                 std::error_code ec;
1072                 std::filesystem::remove(file, ec);
1073             }
1074         }
1075 
1076         // Reload rsyslog so it knows to start new log files
1077         crow::connections::systemBus->async_method_call(
1078             [asyncResp](const boost::system::error_code ec) {
1079             if (ec)
1080             {
1081                 BMCWEB_LOG_ERROR << "Failed to reload rsyslog: " << ec;
1082                 messages::internalError(asyncResp->res);
1083                 return;
1084             }
1085 
1086             messages::success(asyncResp->res);
1087             },
1088             "org.freedesktop.systemd1", "/org/freedesktop/systemd1",
1089             "org.freedesktop.systemd1.Manager", "ReloadUnit", "rsyslog.service",
1090             "replace");
1091         });
1092 }
1093 
1094 enum class LogParseError
1095 {
1096     success,
1097     parseFailed,
1098     messageIdNotInRegistry,
1099 };
1100 
1101 static LogParseError
1102     fillEventLogEntryJson(const std::string& logEntryID,
1103                           const std::string& logEntry,
1104                           nlohmann::json::object_t& logEntryJson)
1105 {
1106     // The redfish log format is "<Timestamp> <MessageId>,<MessageArgs>"
1107     // First get the Timestamp
1108     size_t space = logEntry.find_first_of(' ');
1109     if (space == std::string::npos)
1110     {
1111         return LogParseError::parseFailed;
1112     }
1113     std::string timestamp = logEntry.substr(0, space);
1114     // Then get the log contents
1115     size_t entryStart = logEntry.find_first_not_of(' ', space);
1116     if (entryStart == std::string::npos)
1117     {
1118         return LogParseError::parseFailed;
1119     }
1120     std::string_view entry(logEntry);
1121     entry.remove_prefix(entryStart);
1122     // Use split to separate the entry into its fields
1123     std::vector<std::string> logEntryFields;
1124     boost::split(logEntryFields, entry, boost::is_any_of(","),
1125                  boost::token_compress_on);
1126     // We need at least a MessageId to be valid
1127     if (logEntryFields.empty())
1128     {
1129         return LogParseError::parseFailed;
1130     }
1131     std::string& messageID = logEntryFields[0];
1132 
1133     // Get the Message from the MessageRegistry
1134     const registries::Message* message = registries::getMessage(messageID);
1135 
1136     if (message == nullptr)
1137     {
1138         BMCWEB_LOG_WARNING << "Log entry not found in registry: " << logEntry;
1139         return LogParseError::messageIdNotInRegistry;
1140     }
1141 
1142     std::string msg = message->message;
1143 
1144     // Get the MessageArgs from the log if there are any
1145     std::span<std::string> messageArgs;
1146     if (logEntryFields.size() > 1)
1147     {
1148         std::string& messageArgsStart = logEntryFields[1];
1149         // If the first string is empty, assume there are no MessageArgs
1150         std::size_t messageArgsSize = 0;
1151         if (!messageArgsStart.empty())
1152         {
1153             messageArgsSize = logEntryFields.size() - 1;
1154         }
1155 
1156         messageArgs = {&messageArgsStart, messageArgsSize};
1157 
1158         // Fill the MessageArgs into the Message
1159         int i = 0;
1160         for (const std::string& messageArg : messageArgs)
1161         {
1162             std::string argStr = "%" + std::to_string(++i);
1163             size_t argPos = msg.find(argStr);
1164             if (argPos != std::string::npos)
1165             {
1166                 msg.replace(argPos, argStr.length(), messageArg);
1167             }
1168         }
1169     }
1170 
1171     // Get the Created time from the timestamp. The log timestamp is in RFC3339
1172     // format which matches the Redfish format except for the fractional seconds
1173     // between the '.' and the '+', so just remove them.
1174     std::size_t dot = timestamp.find_first_of('.');
1175     std::size_t plus = timestamp.find_first_of('+');
1176     if (dot != std::string::npos && plus != std::string::npos)
1177     {
1178         timestamp.erase(dot, plus - dot);
1179     }
1180 
1181     // Fill in the log entry with the gathered data
1182     logEntryJson["@odata.type"] = "#LogEntry.v1_9_0.LogEntry";
1183     logEntryJson["@odata.id"] =
1184         "/redfish/v1/Systems/system/LogServices/EventLog/Entries/" + logEntryID;
1185     logEntryJson["Name"] = "System Event Log Entry";
1186     logEntryJson["Id"] = logEntryID;
1187     logEntryJson["Message"] = std::move(msg);
1188     logEntryJson["MessageId"] = std::move(messageID);
1189     logEntryJson["MessageArgs"] = messageArgs;
1190     logEntryJson["EntryType"] = "Event";
1191     logEntryJson["Severity"] = message->messageSeverity;
1192     logEntryJson["Created"] = std::move(timestamp);
1193     return LogParseError::success;
1194 }
1195 
1196 inline void requestRoutesJournalEventLogEntryCollection(App& app)
1197 {
1198     BMCWEB_ROUTE(app, "/redfish/v1/Systems/<str>/LogServices/EventLog/Entries/")
1199         .privileges(redfish::privileges::getLogEntryCollection)
1200         .methods(boost::beast::http::verb::get)(
1201             [&app](const crow::Request& req,
1202                    const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
1203                    const std::string& systemName) {
1204         query_param::QueryCapabilities capabilities = {
1205             .canDelegateTop = true,
1206             .canDelegateSkip = true,
1207         };
1208         query_param::Query delegatedQuery;
1209         if (!redfish::setUpRedfishRouteWithDelegation(
1210                 app, req, asyncResp, delegatedQuery, capabilities))
1211         {
1212             return;
1213         }
1214         if (systemName != "system")
1215         {
1216             messages::resourceNotFound(asyncResp->res, "ComputerSystem",
1217                                        systemName);
1218             return;
1219         }
1220 
1221         size_t top = delegatedQuery.top.value_or(query_param::Query::maxTop);
1222         size_t skip = delegatedQuery.skip.value_or(0);
1223 
1224         // Collections don't include the static data added by SubRoute
1225         // because it has a duplicate entry for members
1226         asyncResp->res.jsonValue["@odata.type"] =
1227             "#LogEntryCollection.LogEntryCollection";
1228         asyncResp->res.jsonValue["@odata.id"] =
1229             "/redfish/v1/Systems/system/LogServices/EventLog/Entries";
1230         asyncResp->res.jsonValue["Name"] = "System Event Log Entries";
1231         asyncResp->res.jsonValue["Description"] =
1232             "Collection of System Event Log Entries";
1233 
1234         nlohmann::json& logEntryArray = asyncResp->res.jsonValue["Members"];
1235         logEntryArray = nlohmann::json::array();
1236         // Go through the log files and create a unique ID for each
1237         // entry
1238         std::vector<std::filesystem::path> redfishLogFiles;
1239         getRedfishLogFiles(redfishLogFiles);
1240         uint64_t entryCount = 0;
1241         std::string logEntry;
1242 
1243         // Oldest logs are in the last file, so start there and loop
1244         // backwards
1245         for (auto it = redfishLogFiles.rbegin(); it < redfishLogFiles.rend();
1246              it++)
1247         {
1248             std::ifstream logStream(*it);
1249             if (!logStream.is_open())
1250             {
1251                 continue;
1252             }
1253 
1254             // Reset the unique ID on the first entry
1255             bool firstEntry = true;
1256             while (std::getline(logStream, logEntry))
1257             {
1258                 std::string idStr;
1259                 if (!getUniqueEntryID(logEntry, idStr, firstEntry))
1260                 {
1261                     continue;
1262                 }
1263                 firstEntry = false;
1264 
1265                 nlohmann::json::object_t bmcLogEntry;
1266                 LogParseError status =
1267                     fillEventLogEntryJson(idStr, logEntry, bmcLogEntry);
1268                 if (status == LogParseError::messageIdNotInRegistry)
1269                 {
1270                     continue;
1271                 }
1272                 if (status != LogParseError::success)
1273                 {
1274                     messages::internalError(asyncResp->res);
1275                     return;
1276                 }
1277 
1278                 entryCount++;
1279                 // Handle paging using skip (number of entries to skip from the
1280                 // start) and top (number of entries to display)
1281                 if (entryCount <= skip || entryCount > skip + top)
1282                 {
1283                     continue;
1284                 }
1285 
1286                 logEntryArray.push_back(std::move(bmcLogEntry));
1287             }
1288         }
1289         asyncResp->res.jsonValue["Members@odata.count"] = entryCount;
1290         if (skip + top < entryCount)
1291         {
1292             asyncResp->res.jsonValue["Members@odata.nextLink"] =
1293                 "/redfish/v1/Systems/system/LogServices/EventLog/Entries?$skip=" +
1294                 std::to_string(skip + top);
1295         }
1296         });
1297 }
1298 
1299 inline void requestRoutesJournalEventLogEntry(App& app)
1300 {
1301     BMCWEB_ROUTE(
1302         app, "/redfish/v1/Systems/<str>/LogServices/EventLog/Entries/<str>/")
1303         .privileges(redfish::privileges::getLogEntry)
1304         .methods(boost::beast::http::verb::get)(
1305             [&app](const crow::Request& req,
1306                    const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
1307                    const std::string& systemName, const std::string& param) {
1308         if (!redfish::setUpRedfishRoute(app, req, asyncResp))
1309         {
1310             return;
1311         }
1312 
1313         if (systemName != "system")
1314         {
1315             messages::resourceNotFound(asyncResp->res, "ComputerSystem",
1316                                        systemName);
1317             return;
1318         }
1319 
1320         const std::string& targetID = param;
1321 
1322         // Go through the log files and check the unique ID for each
1323         // entry to find the target entry
1324         std::vector<std::filesystem::path> redfishLogFiles;
1325         getRedfishLogFiles(redfishLogFiles);
1326         std::string logEntry;
1327 
1328         // Oldest logs are in the last file, so start there and loop
1329         // backwards
1330         for (auto it = redfishLogFiles.rbegin(); it < redfishLogFiles.rend();
1331              it++)
1332         {
1333             std::ifstream logStream(*it);
1334             if (!logStream.is_open())
1335             {
1336                 continue;
1337             }
1338 
1339             // Reset the unique ID on the first entry
1340             bool firstEntry = true;
1341             while (std::getline(logStream, logEntry))
1342             {
1343                 std::string idStr;
1344                 if (!getUniqueEntryID(logEntry, idStr, firstEntry))
1345                 {
1346                     continue;
1347                 }
1348                 firstEntry = false;
1349 
1350                 if (idStr == targetID)
1351                 {
1352                     nlohmann::json::object_t bmcLogEntry;
1353                     LogParseError status =
1354                         fillEventLogEntryJson(idStr, logEntry, bmcLogEntry);
1355                     if (status != LogParseError::success)
1356                     {
1357                         messages::internalError(asyncResp->res);
1358                         return;
1359                     }
1360                     asyncResp->res.jsonValue.update(bmcLogEntry);
1361                     return;
1362                 }
1363             }
1364         }
1365         // Requested ID was not found
1366         messages::resourceMissingAtURI(asyncResp->res,
1367                                        crow::utility::urlFromPieces(targetID));
1368         });
1369 }
1370 
1371 inline void requestRoutesDBusEventLogEntryCollection(App& app)
1372 {
1373     BMCWEB_ROUTE(app, "/redfish/v1/Systems/<str>/LogServices/EventLog/Entries/")
1374         .privileges(redfish::privileges::getLogEntryCollection)
1375         .methods(boost::beast::http::verb::get)(
1376             [&app](const crow::Request& req,
1377                    const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
1378                    const std::string& systemName) {
1379         if (!redfish::setUpRedfishRoute(app, req, asyncResp))
1380         {
1381             return;
1382         }
1383         if (systemName != "system")
1384         {
1385             messages::resourceNotFound(asyncResp->res, "ComputerSystem",
1386                                        systemName);
1387             return;
1388         }
1389 
1390         // Collections don't include the static data added by SubRoute
1391         // because it has a duplicate entry for members
1392         asyncResp->res.jsonValue["@odata.type"] =
1393             "#LogEntryCollection.LogEntryCollection";
1394         asyncResp->res.jsonValue["@odata.id"] =
1395             "/redfish/v1/Systems/system/LogServices/EventLog/Entries";
1396         asyncResp->res.jsonValue["Name"] = "System Event Log Entries";
1397         asyncResp->res.jsonValue["Description"] =
1398             "Collection of System Event Log Entries";
1399 
1400         // DBus implementation of EventLog/Entries
1401         // Make call to Logging Service to find all log entry objects
1402         crow::connections::systemBus->async_method_call(
1403             [asyncResp](const boost::system::error_code ec,
1404                         const dbus::utility::ManagedObjectType& resp) {
1405             if (ec)
1406             {
1407                 // TODO Handle for specific error code
1408                 BMCWEB_LOG_ERROR
1409                     << "getLogEntriesIfaceData resp_handler got error " << ec;
1410                 messages::internalError(asyncResp->res);
1411                 return;
1412             }
1413             nlohmann::json& entriesArray = asyncResp->res.jsonValue["Members"];
1414             entriesArray = nlohmann::json::array();
1415             for (const auto& objectPath : resp)
1416             {
1417                 const uint32_t* id = nullptr;
1418                 const uint64_t* timestamp = nullptr;
1419                 const uint64_t* updateTimestamp = nullptr;
1420                 const std::string* severity = nullptr;
1421                 const std::string* message = nullptr;
1422                 const std::string* filePath = nullptr;
1423                 const std::string* resolution = nullptr;
1424                 bool resolved = false;
1425                 for (const auto& interfaceMap : objectPath.second)
1426                 {
1427                     if (interfaceMap.first ==
1428                         "xyz.openbmc_project.Logging.Entry")
1429                     {
1430                         for (const auto& propertyMap : interfaceMap.second)
1431                         {
1432                             if (propertyMap.first == "Id")
1433                             {
1434                                 id = std::get_if<uint32_t>(&propertyMap.second);
1435                             }
1436                             else if (propertyMap.first == "Timestamp")
1437                             {
1438                                 timestamp =
1439                                     std::get_if<uint64_t>(&propertyMap.second);
1440                             }
1441                             else if (propertyMap.first == "UpdateTimestamp")
1442                             {
1443                                 updateTimestamp =
1444                                     std::get_if<uint64_t>(&propertyMap.second);
1445                             }
1446                             else if (propertyMap.first == "Severity")
1447                             {
1448                                 severity = std::get_if<std::string>(
1449                                     &propertyMap.second);
1450                             }
1451                             else if (propertyMap.first == "Resolution")
1452                             {
1453                                 resolution = std::get_if<std::string>(
1454                                     &propertyMap.second);
1455                             }
1456                             else if (propertyMap.first == "Message")
1457                             {
1458                                 message = std::get_if<std::string>(
1459                                     &propertyMap.second);
1460                             }
1461                             else if (propertyMap.first == "Resolved")
1462                             {
1463                                 const bool* resolveptr =
1464                                     std::get_if<bool>(&propertyMap.second);
1465                                 if (resolveptr == nullptr)
1466                                 {
1467                                     messages::internalError(asyncResp->res);
1468                                     return;
1469                                 }
1470                                 resolved = *resolveptr;
1471                             }
1472                         }
1473                         if (id == nullptr || message == nullptr ||
1474                             severity == nullptr)
1475                         {
1476                             messages::internalError(asyncResp->res);
1477                             return;
1478                         }
1479                     }
1480                     else if (interfaceMap.first ==
1481                              "xyz.openbmc_project.Common.FilePath")
1482                     {
1483                         for (const auto& propertyMap : interfaceMap.second)
1484                         {
1485                             if (propertyMap.first == "Path")
1486                             {
1487                                 filePath = std::get_if<std::string>(
1488                                     &propertyMap.second);
1489                             }
1490                         }
1491                     }
1492                 }
1493                 // Object path without the
1494                 // xyz.openbmc_project.Logging.Entry interface, ignore
1495                 // and continue.
1496                 if (id == nullptr || message == nullptr ||
1497                     severity == nullptr || timestamp == nullptr ||
1498                     updateTimestamp == nullptr)
1499                 {
1500                     continue;
1501                 }
1502                 entriesArray.push_back({});
1503                 nlohmann::json& thisEntry = entriesArray.back();
1504                 thisEntry["@odata.type"] = "#LogEntry.v1_9_0.LogEntry";
1505                 thisEntry["@odata.id"] =
1506                     "/redfish/v1/Systems/system/LogServices/EventLog/Entries/" +
1507                     std::to_string(*id);
1508                 thisEntry["Name"] = "System Event Log Entry";
1509                 thisEntry["Id"] = std::to_string(*id);
1510                 thisEntry["Message"] = *message;
1511                 thisEntry["Resolved"] = resolved;
1512                 if ((resolution != nullptr) && (!(*resolution).empty()))
1513                 {
1514                     thisEntry["Resolution"] = *resolution;
1515                 }
1516                 thisEntry["EntryType"] = "Event";
1517                 thisEntry["Severity"] =
1518                     translateSeverityDbusToRedfish(*severity);
1519                 thisEntry["Created"] =
1520                     redfish::time_utils::getDateTimeUintMs(*timestamp);
1521                 thisEntry["Modified"] =
1522                     redfish::time_utils::getDateTimeUintMs(*updateTimestamp);
1523                 if (filePath != nullptr)
1524                 {
1525                     thisEntry["AdditionalDataURI"] =
1526                         "/redfish/v1/Systems/system/LogServices/EventLog/Entries/" +
1527                         std::to_string(*id) + "/attachment";
1528                 }
1529             }
1530             std::sort(
1531                 entriesArray.begin(), entriesArray.end(),
1532                 [](const nlohmann::json& left, const nlohmann::json& right) {
1533                 return (left["Id"] <= right["Id"]);
1534                 });
1535             asyncResp->res.jsonValue["Members@odata.count"] =
1536                 entriesArray.size();
1537             },
1538             "xyz.openbmc_project.Logging", "/xyz/openbmc_project/logging",
1539             "org.freedesktop.DBus.ObjectManager", "GetManagedObjects");
1540         });
1541 }
1542 
1543 inline void requestRoutesDBusEventLogEntry(App& app)
1544 {
1545     BMCWEB_ROUTE(
1546         app, "/redfish/v1/Systems/<str>/LogServices/EventLog/Entries/<str>/")
1547         .privileges(redfish::privileges::getLogEntry)
1548         .methods(boost::beast::http::verb::get)(
1549             [&app](const crow::Request& req,
1550                    const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
1551                    const std::string& systemName, const std::string& param) {
1552         if (!redfish::setUpRedfishRoute(app, req, asyncResp))
1553         {
1554             return;
1555         }
1556         if (systemName != "system")
1557         {
1558             messages::resourceNotFound(asyncResp->res, "ComputerSystem",
1559                                        systemName);
1560             return;
1561         }
1562 
1563         std::string entryID = param;
1564         dbus::utility::escapePathForDbus(entryID);
1565 
1566         // DBus implementation of EventLog/Entries
1567         // Make call to Logging Service to find all log entry objects
1568         sdbusplus::asio::getAllProperties(
1569             *crow::connections::systemBus, "xyz.openbmc_project.Logging",
1570             "/xyz/openbmc_project/logging/entry/" + entryID, "",
1571             [asyncResp, entryID](const boost::system::error_code ec,
1572                                  const dbus::utility::DBusPropertiesMap& resp) {
1573             if (ec.value() == EBADR)
1574             {
1575                 messages::resourceNotFound(asyncResp->res, "EventLogEntry",
1576                                            entryID);
1577                 return;
1578             }
1579             if (ec)
1580             {
1581                 BMCWEB_LOG_ERROR
1582                     << "EventLogEntry (DBus) resp_handler got error " << ec;
1583                 messages::internalError(asyncResp->res);
1584                 return;
1585             }
1586             const uint32_t* id = nullptr;
1587             const uint64_t* timestamp = nullptr;
1588             const uint64_t* updateTimestamp = nullptr;
1589             const std::string* severity = nullptr;
1590             const std::string* message = nullptr;
1591             const std::string* filePath = nullptr;
1592             const std::string* resolution = nullptr;
1593             bool resolved = false;
1594 
1595             const bool success = sdbusplus::unpackPropertiesNoThrow(
1596                 dbus_utils::UnpackErrorPrinter(), resp, "Id", id, "Timestamp",
1597                 timestamp, "UpdateTimestamp", updateTimestamp, "Severity",
1598                 severity, "Message", message, "Resolved", resolved,
1599                 "Resolution", resolution, "Path", filePath);
1600 
1601             if (!success)
1602             {
1603                 messages::internalError(asyncResp->res);
1604                 return;
1605             }
1606 
1607             if (id == nullptr || message == nullptr || severity == nullptr ||
1608                 timestamp == nullptr || updateTimestamp == nullptr)
1609             {
1610                 messages::internalError(asyncResp->res);
1611                 return;
1612             }
1613             asyncResp->res.jsonValue["@odata.type"] =
1614                 "#LogEntry.v1_9_0.LogEntry";
1615             asyncResp->res.jsonValue["@odata.id"] =
1616                 "/redfish/v1/Systems/system/LogServices/EventLog/Entries/" +
1617                 std::to_string(*id);
1618             asyncResp->res.jsonValue["Name"] = "System Event Log Entry";
1619             asyncResp->res.jsonValue["Id"] = std::to_string(*id);
1620             asyncResp->res.jsonValue["Message"] = *message;
1621             asyncResp->res.jsonValue["Resolved"] = resolved;
1622             if ((resolution != nullptr) && (!(*resolution).empty()))
1623             {
1624                 asyncResp->res.jsonValue["Resolution"] = *resolution;
1625             }
1626             asyncResp->res.jsonValue["EntryType"] = "Event";
1627             asyncResp->res.jsonValue["Severity"] =
1628                 translateSeverityDbusToRedfish(*severity);
1629             asyncResp->res.jsonValue["Created"] =
1630                 redfish::time_utils::getDateTimeUintMs(*timestamp);
1631             asyncResp->res.jsonValue["Modified"] =
1632                 redfish::time_utils::getDateTimeUintMs(*updateTimestamp);
1633             if (filePath != nullptr)
1634             {
1635                 asyncResp->res.jsonValue["AdditionalDataURI"] =
1636                     "/redfish/v1/Systems/system/LogServices/EventLog/Entries/" +
1637                     std::to_string(*id) + "/attachment";
1638             }
1639             });
1640         });
1641 
1642     BMCWEB_ROUTE(
1643         app, "/redfish/v1/Systems/<str>/LogServices/EventLog/Entries/<str>/")
1644         .privileges(redfish::privileges::patchLogEntry)
1645         .methods(boost::beast::http::verb::patch)(
1646             [&app](const crow::Request& req,
1647                    const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
1648                    const std::string& systemName, const std::string& entryId) {
1649         if (!redfish::setUpRedfishRoute(app, req, asyncResp))
1650         {
1651             return;
1652         }
1653         if (systemName != "system")
1654         {
1655             messages::resourceNotFound(asyncResp->res, "ComputerSystem",
1656                                        systemName);
1657             return;
1658         }
1659         std::optional<bool> resolved;
1660 
1661         if (!json_util::readJsonPatch(req, asyncResp->res, "Resolved",
1662                                       resolved))
1663         {
1664             return;
1665         }
1666         BMCWEB_LOG_DEBUG << "Set Resolved";
1667 
1668         crow::connections::systemBus->async_method_call(
1669             [asyncResp, entryId](const boost::system::error_code ec) {
1670             if (ec)
1671             {
1672                 BMCWEB_LOG_DEBUG << "DBUS response error " << ec;
1673                 messages::internalError(asyncResp->res);
1674                 return;
1675             }
1676             },
1677             "xyz.openbmc_project.Logging",
1678             "/xyz/openbmc_project/logging/entry/" + entryId,
1679             "org.freedesktop.DBus.Properties", "Set",
1680             "xyz.openbmc_project.Logging.Entry", "Resolved",
1681             dbus::utility::DbusVariantType(*resolved));
1682         });
1683 
1684     BMCWEB_ROUTE(
1685         app, "/redfish/v1/Systems/<str>/LogServices/EventLog/Entries/<str>/")
1686         .privileges(redfish::privileges::deleteLogEntry)
1687 
1688         .methods(boost::beast::http::verb::delete_)(
1689             [&app](const crow::Request& req,
1690                    const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
1691                    const std::string& systemName, const std::string& param) {
1692         if (!redfish::setUpRedfishRoute(app, req, asyncResp))
1693         {
1694             return;
1695         }
1696         if (systemName != "system")
1697         {
1698             messages::resourceNotFound(asyncResp->res, "ComputerSystem",
1699                                        systemName);
1700             return;
1701         }
1702         BMCWEB_LOG_DEBUG << "Do delete single event entries.";
1703 
1704         std::string entryID = param;
1705 
1706         dbus::utility::escapePathForDbus(entryID);
1707 
1708         // Process response from Logging service.
1709         auto respHandler =
1710             [asyncResp, entryID](const boost::system::error_code ec) {
1711             BMCWEB_LOG_DEBUG << "EventLogEntry (DBus) doDelete callback: Done";
1712             if (ec)
1713             {
1714                 if (ec.value() == EBADR)
1715                 {
1716                     messages::resourceNotFound(asyncResp->res, "LogEntry",
1717                                                entryID);
1718                     return;
1719                 }
1720                 // TODO Handle for specific error code
1721                 BMCWEB_LOG_ERROR
1722                     << "EventLogEntry (DBus) doDelete respHandler got error "
1723                     << ec;
1724                 asyncResp->res.result(
1725                     boost::beast::http::status::internal_server_error);
1726                 return;
1727             }
1728 
1729             asyncResp->res.result(boost::beast::http::status::ok);
1730         };
1731 
1732         // Make call to Logging service to request Delete Log
1733         crow::connections::systemBus->async_method_call(
1734             respHandler, "xyz.openbmc_project.Logging",
1735             "/xyz/openbmc_project/logging/entry/" + entryID,
1736             "xyz.openbmc_project.Object.Delete", "Delete");
1737         });
1738 }
1739 
1740 inline void requestRoutesDBusEventLogEntryDownload(App& app)
1741 {
1742     BMCWEB_ROUTE(
1743         app,
1744         "/redfish/v1/Systems/<str>/LogServices/EventLog/Entries/<str>/attachment")
1745         .privileges(redfish::privileges::getLogEntry)
1746         .methods(boost::beast::http::verb::get)(
1747             [&app](const crow::Request& req,
1748                    const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
1749                    const std::string& systemName, const std::string& param) {
1750         if (!redfish::setUpRedfishRoute(app, req, asyncResp))
1751         {
1752             return;
1753         }
1754         if (http_helpers::isContentTypeAllowed(
1755                 req.getHeaderValue("Accept"),
1756                 http_helpers::ContentType::OctetStream, true))
1757         {
1758             asyncResp->res.result(boost::beast::http::status::bad_request);
1759             return;
1760         }
1761         if (systemName != "system")
1762         {
1763             messages::resourceNotFound(asyncResp->res, "ComputerSystem",
1764                                        systemName);
1765             return;
1766         }
1767 
1768         std::string entryID = param;
1769         dbus::utility::escapePathForDbus(entryID);
1770 
1771         crow::connections::systemBus->async_method_call(
1772             [asyncResp, entryID](const boost::system::error_code ec,
1773                                  const sdbusplus::message::unix_fd& unixfd) {
1774             if (ec.value() == EBADR)
1775             {
1776                 messages::resourceNotFound(asyncResp->res, "EventLogAttachment",
1777                                            entryID);
1778                 return;
1779             }
1780             if (ec)
1781             {
1782                 BMCWEB_LOG_DEBUG << "DBUS response error " << ec;
1783                 messages::internalError(asyncResp->res);
1784                 return;
1785             }
1786 
1787             int fd = -1;
1788             fd = dup(unixfd);
1789             if (fd == -1)
1790             {
1791                 messages::internalError(asyncResp->res);
1792                 return;
1793             }
1794 
1795             long long int size = lseek(fd, 0, SEEK_END);
1796             if (size == -1)
1797             {
1798                 messages::internalError(asyncResp->res);
1799                 return;
1800             }
1801 
1802             // Arbitrary max size of 64kb
1803             constexpr int maxFileSize = 65536;
1804             if (size > maxFileSize)
1805             {
1806                 BMCWEB_LOG_ERROR << "File size exceeds maximum allowed size of "
1807                                  << maxFileSize;
1808                 messages::internalError(asyncResp->res);
1809                 return;
1810             }
1811             std::vector<char> data(static_cast<size_t>(size));
1812             long long int rc = lseek(fd, 0, SEEK_SET);
1813             if (rc == -1)
1814             {
1815                 messages::internalError(asyncResp->res);
1816                 return;
1817             }
1818             rc = read(fd, data.data(), data.size());
1819             if ((rc == -1) || (rc != size))
1820             {
1821                 messages::internalError(asyncResp->res);
1822                 return;
1823             }
1824             close(fd);
1825 
1826             std::string_view strData(data.data(), data.size());
1827             std::string output = crow::utility::base64encode(strData);
1828 
1829             asyncResp->res.addHeader(boost::beast::http::field::content_type,
1830                                      "application/octet-stream");
1831             asyncResp->res.addHeader(
1832                 boost::beast::http::field::content_transfer_encoding, "Base64");
1833             asyncResp->res.body() = std::move(output);
1834             },
1835             "xyz.openbmc_project.Logging",
1836             "/xyz/openbmc_project/logging/entry/" + entryID,
1837             "xyz.openbmc_project.Logging.Entry", "GetEntry");
1838         });
1839 }
1840 
1841 constexpr const char* hostLoggerFolderPath = "/var/log/console";
1842 
1843 inline bool
1844     getHostLoggerFiles(const std::string& hostLoggerFilePath,
1845                        std::vector<std::filesystem::path>& hostLoggerFiles)
1846 {
1847     std::error_code ec;
1848     std::filesystem::directory_iterator logPath(hostLoggerFilePath, ec);
1849     if (ec)
1850     {
1851         BMCWEB_LOG_ERROR << ec.message();
1852         return false;
1853     }
1854     for (const std::filesystem::directory_entry& it : logPath)
1855     {
1856         std::string filename = it.path().filename();
1857         // Prefix of each log files is "log". Find the file and save the
1858         // path
1859         if (filename.starts_with("log"))
1860         {
1861             hostLoggerFiles.emplace_back(it.path());
1862         }
1863     }
1864     // As the log files rotate, they are appended with a ".#" that is higher for
1865     // the older logs. Since we start from oldest logs, sort the name in
1866     // descending order.
1867     std::sort(hostLoggerFiles.rbegin(), hostLoggerFiles.rend(),
1868               AlphanumLess<std::string>());
1869 
1870     return true;
1871 }
1872 
1873 inline bool getHostLoggerEntries(
1874     const std::vector<std::filesystem::path>& hostLoggerFiles, uint64_t skip,
1875     uint64_t top, std::vector<std::string>& logEntries, size_t& logCount)
1876 {
1877     GzFileReader logFile;
1878 
1879     // Go though all log files and expose host logs.
1880     for (const std::filesystem::path& it : hostLoggerFiles)
1881     {
1882         if (!logFile.gzGetLines(it.string(), skip, top, logEntries, logCount))
1883         {
1884             BMCWEB_LOG_ERROR << "fail to expose host logs";
1885             return false;
1886         }
1887     }
1888     // Get lastMessage from constructor by getter
1889     std::string lastMessage = logFile.getLastMessage();
1890     if (!lastMessage.empty())
1891     {
1892         logCount++;
1893         if (logCount > skip && logCount <= (skip + top))
1894         {
1895             logEntries.push_back(lastMessage);
1896         }
1897     }
1898     return true;
1899 }
1900 
1901 inline void fillHostLoggerEntryJson(const std::string& logEntryID,
1902                                     const std::string& msg,
1903                                     nlohmann::json::object_t& logEntryJson)
1904 {
1905     // Fill in the log entry with the gathered data.
1906     logEntryJson["@odata.type"] = "#LogEntry.v1_9_0.LogEntry";
1907     logEntryJson["@odata.id"] =
1908         "/redfish/v1/Systems/system/LogServices/HostLogger/Entries/" +
1909         logEntryID;
1910     logEntryJson["Name"] = "Host Logger Entry";
1911     logEntryJson["Id"] = logEntryID;
1912     logEntryJson["Message"] = msg;
1913     logEntryJson["EntryType"] = "Oem";
1914     logEntryJson["Severity"] = "OK";
1915     logEntryJson["OemRecordFormat"] = "Host Logger Entry";
1916 }
1917 
1918 inline void requestRoutesSystemHostLogger(App& app)
1919 {
1920     BMCWEB_ROUTE(app, "/redfish/v1/Systems/<str>/LogServices/HostLogger/")
1921         .privileges(redfish::privileges::getLogService)
1922         .methods(boost::beast::http::verb::get)(
1923             [&app](const crow::Request& req,
1924                    const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
1925                    const std::string& systemName) {
1926         if (!redfish::setUpRedfishRoute(app, req, asyncResp))
1927         {
1928             return;
1929         }
1930         if (systemName != "system")
1931         {
1932             messages::resourceNotFound(asyncResp->res, "ComputerSystem",
1933                                        systemName);
1934             return;
1935         }
1936         asyncResp->res.jsonValue["@odata.id"] =
1937             "/redfish/v1/Systems/system/LogServices/HostLogger";
1938         asyncResp->res.jsonValue["@odata.type"] =
1939             "#LogService.v1_1_0.LogService";
1940         asyncResp->res.jsonValue["Name"] = "Host Logger Service";
1941         asyncResp->res.jsonValue["Description"] = "Host Logger Service";
1942         asyncResp->res.jsonValue["Id"] = "HostLogger";
1943         asyncResp->res.jsonValue["Entries"]["@odata.id"] =
1944             "/redfish/v1/Systems/system/LogServices/HostLogger/Entries";
1945         });
1946 }
1947 
1948 inline void requestRoutesSystemHostLoggerCollection(App& app)
1949 {
1950     BMCWEB_ROUTE(app,
1951                  "/redfish/v1/Systems/<str>/LogServices/HostLogger/Entries/")
1952         .privileges(redfish::privileges::getLogEntry)
1953         .methods(boost::beast::http::verb::get)(
1954             [&app](const crow::Request& req,
1955                    const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
1956                    const std::string& systemName) {
1957         query_param::QueryCapabilities capabilities = {
1958             .canDelegateTop = true,
1959             .canDelegateSkip = true,
1960         };
1961         query_param::Query delegatedQuery;
1962         if (!redfish::setUpRedfishRouteWithDelegation(
1963                 app, req, asyncResp, delegatedQuery, capabilities))
1964         {
1965             return;
1966         }
1967         if (systemName != "system")
1968         {
1969             messages::resourceNotFound(asyncResp->res, "ComputerSystem",
1970                                        systemName);
1971             return;
1972         }
1973         asyncResp->res.jsonValue["@odata.id"] =
1974             "/redfish/v1/Systems/system/LogServices/HostLogger/Entries";
1975         asyncResp->res.jsonValue["@odata.type"] =
1976             "#LogEntryCollection.LogEntryCollection";
1977         asyncResp->res.jsonValue["Name"] = "HostLogger Entries";
1978         asyncResp->res.jsonValue["Description"] =
1979             "Collection of HostLogger Entries";
1980         nlohmann::json& logEntryArray = asyncResp->res.jsonValue["Members"];
1981         logEntryArray = nlohmann::json::array();
1982         asyncResp->res.jsonValue["Members@odata.count"] = 0;
1983 
1984         std::vector<std::filesystem::path> hostLoggerFiles;
1985         if (!getHostLoggerFiles(hostLoggerFolderPath, hostLoggerFiles))
1986         {
1987             BMCWEB_LOG_ERROR << "fail to get host log file path";
1988             return;
1989         }
1990         // If we weren't provided top and skip limits, use the defaults.
1991         size_t skip = delegatedQuery.skip.value_or(0);
1992         size_t top = delegatedQuery.top.value_or(query_param::Query::maxTop);
1993         size_t logCount = 0;
1994         // This vector only store the entries we want to expose that
1995         // control by skip and top.
1996         std::vector<std::string> logEntries;
1997         if (!getHostLoggerEntries(hostLoggerFiles, skip, top, logEntries,
1998                                   logCount))
1999         {
2000             messages::internalError(asyncResp->res);
2001             return;
2002         }
2003         // If vector is empty, that means skip value larger than total
2004         // log count
2005         if (logEntries.empty())
2006         {
2007             asyncResp->res.jsonValue["Members@odata.count"] = logCount;
2008             return;
2009         }
2010         if (!logEntries.empty())
2011         {
2012             for (size_t i = 0; i < logEntries.size(); i++)
2013             {
2014                 nlohmann::json::object_t hostLogEntry;
2015                 fillHostLoggerEntryJson(std::to_string(skip + i), logEntries[i],
2016                                         hostLogEntry);
2017                 logEntryArray.push_back(std::move(hostLogEntry));
2018             }
2019 
2020             asyncResp->res.jsonValue["Members@odata.count"] = logCount;
2021             if (skip + top < logCount)
2022             {
2023                 asyncResp->res.jsonValue["Members@odata.nextLink"] =
2024                     "/redfish/v1/Systems/system/LogServices/HostLogger/Entries?$skip=" +
2025                     std::to_string(skip + top);
2026             }
2027         }
2028         });
2029 }
2030 
2031 inline void requestRoutesSystemHostLoggerLogEntry(App& app)
2032 {
2033     BMCWEB_ROUTE(
2034         app, "/redfish/v1/Systems/<str>/LogServices/HostLogger/Entries/<str>/")
2035         .privileges(redfish::privileges::getLogEntry)
2036         .methods(boost::beast::http::verb::get)(
2037             [&app](const crow::Request& req,
2038                    const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
2039                    const std::string& systemName, const std::string& param) {
2040         if (!redfish::setUpRedfishRoute(app, req, asyncResp))
2041         {
2042             return;
2043         }
2044         if (systemName != "system")
2045         {
2046             messages::resourceNotFound(asyncResp->res, "ComputerSystem",
2047                                        systemName);
2048             return;
2049         }
2050         const std::string& targetID = param;
2051 
2052         uint64_t idInt = 0;
2053 
2054         // NOLINTNEXTLINE(cppcoreguidelines-pro-bounds-pointer-arithmetic)
2055         const char* end = targetID.data() + targetID.size();
2056 
2057         auto [ptr, ec] = std::from_chars(targetID.data(), end, idInt);
2058         if (ec == std::errc::invalid_argument)
2059         {
2060             messages::resourceMissingAtURI(asyncResp->res, req.urlView);
2061             return;
2062         }
2063         if (ec == std::errc::result_out_of_range)
2064         {
2065             messages::resourceMissingAtURI(asyncResp->res, req.urlView);
2066             return;
2067         }
2068 
2069         std::vector<std::filesystem::path> hostLoggerFiles;
2070         if (!getHostLoggerFiles(hostLoggerFolderPath, hostLoggerFiles))
2071         {
2072             BMCWEB_LOG_ERROR << "fail to get host log file path";
2073             return;
2074         }
2075 
2076         size_t logCount = 0;
2077         size_t top = 1;
2078         std::vector<std::string> logEntries;
2079         // We can get specific entry by skip and top. For example, if we
2080         // want to get nth entry, we can set skip = n-1 and top = 1 to
2081         // get that entry
2082         if (!getHostLoggerEntries(hostLoggerFiles, idInt, top, logEntries,
2083                                   logCount))
2084         {
2085             messages::internalError(asyncResp->res);
2086             return;
2087         }
2088 
2089         if (!logEntries.empty())
2090         {
2091             nlohmann::json::object_t hostLogEntry;
2092             fillHostLoggerEntryJson(targetID, logEntries[0], hostLogEntry);
2093             asyncResp->res.jsonValue.update(hostLogEntry);
2094             return;
2095         }
2096 
2097         // Requested ID was not found
2098         messages::resourceMissingAtURI(asyncResp->res, req.urlView);
2099         });
2100 }
2101 
2102 constexpr char const* dumpManagerIface =
2103     "xyz.openbmc_project.Collection.DeleteAll";
2104 inline void handleBMCLogServicesCollectionGet(
2105     crow::App& app, const crow::Request& req,
2106     const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
2107 {
2108     if (!redfish::setUpRedfishRoute(app, req, asyncResp))
2109     {
2110         return;
2111     }
2112     // Collections don't include the static data added by SubRoute
2113     // because it has a duplicate entry for members
2114     asyncResp->res.jsonValue["@odata.type"] =
2115         "#LogServiceCollection.LogServiceCollection";
2116     asyncResp->res.jsonValue["@odata.id"] =
2117         "/redfish/v1/Managers/bmc/LogServices";
2118     asyncResp->res.jsonValue["Name"] = "Open BMC Log Services Collection";
2119     asyncResp->res.jsonValue["Description"] =
2120         "Collection of LogServices for this Manager";
2121     nlohmann::json& logServiceArray = asyncResp->res.jsonValue["Members"];
2122     logServiceArray = nlohmann::json::array();
2123 
2124 #ifdef BMCWEB_ENABLE_REDFISH_BMC_JOURNAL
2125     nlohmann::json::object_t journal;
2126     journal["@odata.id"] = "/redfish/v1/Managers/bmc/LogServices/Journal";
2127     logServiceArray.push_back(std::move(journal));
2128 #endif
2129 
2130     asyncResp->res.jsonValue["Members@odata.count"] = logServiceArray.size();
2131 
2132 #ifdef BMCWEB_ENABLE_REDFISH_DUMP_LOG
2133     auto respHandler =
2134         [asyncResp](
2135             const boost::system::error_code ec,
2136             const dbus::utility::MapperGetSubTreePathsResponse& subTreePaths) {
2137         if (ec)
2138         {
2139             BMCWEB_LOG_ERROR
2140                 << "handleBMCLogServicesCollectionGet respHandler got error "
2141                 << ec;
2142             // Assume that getting an error simply means there are no dump
2143             // LogServices. Return without adding any error response.
2144             return;
2145         }
2146 
2147         nlohmann::json& logServiceArrayLocal =
2148             asyncResp->res.jsonValue["Members"];
2149 
2150         for (const std::string& path : subTreePaths)
2151         {
2152             if (path == "/xyz/openbmc_project/dump/bmc")
2153             {
2154                 nlohmann::json::object_t member;
2155                 member["@odata.id"] =
2156                     "/redfish/v1/Managers/bmc/LogServices/Dump";
2157                 logServiceArrayLocal.push_back(std::move(member));
2158             }
2159             else if (path == "/xyz/openbmc_project/dump/faultlog")
2160             {
2161                 nlohmann::json::object_t member;
2162                 member["@odata.id"] =
2163                     "/redfish/v1/Managers/bmc/LogServices/FaultLog";
2164                 logServiceArrayLocal.push_back(std::move(member));
2165             }
2166         }
2167 
2168         asyncResp->res.jsonValue["Members@odata.count"] =
2169             logServiceArrayLocal.size();
2170     };
2171 
2172     crow::connections::systemBus->async_method_call(
2173         respHandler, "xyz.openbmc_project.ObjectMapper",
2174         "/xyz/openbmc_project/object_mapper",
2175         "xyz.openbmc_project.ObjectMapper", "GetSubTreePaths",
2176         "/xyz/openbmc_project/dump", 0,
2177         std::array<const char*, 1>{dumpManagerIface});
2178 #endif
2179 }
2180 
2181 inline void requestRoutesBMCLogServiceCollection(App& app)
2182 {
2183     BMCWEB_ROUTE(app, "/redfish/v1/Managers/bmc/LogServices/")
2184         .privileges(redfish::privileges::getLogServiceCollection)
2185         .methods(boost::beast::http::verb::get)(
2186             std::bind_front(handleBMCLogServicesCollectionGet, std::ref(app)));
2187 }
2188 
2189 inline void requestRoutesBMCJournalLogService(App& app)
2190 {
2191     BMCWEB_ROUTE(app, "/redfish/v1/Managers/bmc/LogServices/Journal/")
2192         .privileges(redfish::privileges::getLogService)
2193         .methods(boost::beast::http::verb::get)(
2194             [&app](const crow::Request& req,
2195                    const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) {
2196         if (!redfish::setUpRedfishRoute(app, req, asyncResp))
2197         {
2198             return;
2199         }
2200         asyncResp->res.jsonValue["@odata.type"] =
2201             "#LogService.v1_1_0.LogService";
2202         asyncResp->res.jsonValue["@odata.id"] =
2203             "/redfish/v1/Managers/bmc/LogServices/Journal";
2204         asyncResp->res.jsonValue["Name"] = "Open BMC Journal Log Service";
2205         asyncResp->res.jsonValue["Description"] = "BMC Journal Log Service";
2206         asyncResp->res.jsonValue["Id"] = "BMC Journal";
2207         asyncResp->res.jsonValue["OverWritePolicy"] = "WrapsWhenFull";
2208 
2209         std::pair<std::string, std::string> redfishDateTimeOffset =
2210             redfish::time_utils::getDateTimeOffsetNow();
2211         asyncResp->res.jsonValue["DateTime"] = redfishDateTimeOffset.first;
2212         asyncResp->res.jsonValue["DateTimeLocalOffset"] =
2213             redfishDateTimeOffset.second;
2214 
2215         asyncResp->res.jsonValue["Entries"]["@odata.id"] =
2216             "/redfish/v1/Managers/bmc/LogServices/Journal/Entries";
2217         });
2218 }
2219 
2220 static int
2221     fillBMCJournalLogEntryJson(const std::string& bmcJournalLogEntryID,
2222                                sd_journal* journal,
2223                                nlohmann::json::object_t& bmcJournalLogEntryJson)
2224 {
2225     // Get the Log Entry contents
2226     int ret = 0;
2227 
2228     std::string message;
2229     std::string_view syslogID;
2230     ret = getJournalMetadata(journal, "SYSLOG_IDENTIFIER", syslogID);
2231     if (ret < 0)
2232     {
2233         BMCWEB_LOG_ERROR << "Failed to read SYSLOG_IDENTIFIER field: "
2234                          << strerror(-ret);
2235     }
2236     if (!syslogID.empty())
2237     {
2238         message += std::string(syslogID) + ": ";
2239     }
2240 
2241     std::string_view msg;
2242     ret = getJournalMetadata(journal, "MESSAGE", msg);
2243     if (ret < 0)
2244     {
2245         BMCWEB_LOG_ERROR << "Failed to read MESSAGE field: " << strerror(-ret);
2246         return 1;
2247     }
2248     message += std::string(msg);
2249 
2250     // Get the severity from the PRIORITY field
2251     long int severity = 8; // Default to an invalid priority
2252     ret = getJournalMetadata(journal, "PRIORITY", 10, severity);
2253     if (ret < 0)
2254     {
2255         BMCWEB_LOG_ERROR << "Failed to read PRIORITY field: " << strerror(-ret);
2256     }
2257 
2258     // Get the Created time from the timestamp
2259     std::string entryTimeStr;
2260     if (!getEntryTimestamp(journal, entryTimeStr))
2261     {
2262         return 1;
2263     }
2264 
2265     // Fill in the log entry with the gathered data
2266     bmcJournalLogEntryJson["@odata.type"] = "#LogEntry.v1_9_0.LogEntry";
2267     bmcJournalLogEntryJson["@odata.id"] =
2268         "/redfish/v1/Managers/bmc/LogServices/Journal/Entries/" +
2269         bmcJournalLogEntryID;
2270     bmcJournalLogEntryJson["Name"] = "BMC Journal Entry";
2271     bmcJournalLogEntryJson["Id"] = bmcJournalLogEntryID;
2272     bmcJournalLogEntryJson["Message"] = std::move(message);
2273     bmcJournalLogEntryJson["EntryType"] = "Oem";
2274     bmcJournalLogEntryJson["Severity"] = severity <= 2   ? "Critical"
2275                                          : severity <= 4 ? "Warning"
2276                                                          : "OK";
2277     bmcJournalLogEntryJson["OemRecordFormat"] = "BMC Journal Entry";
2278     bmcJournalLogEntryJson["Created"] = std::move(entryTimeStr);
2279     return 0;
2280 }
2281 
2282 inline void requestRoutesBMCJournalLogEntryCollection(App& app)
2283 {
2284     BMCWEB_ROUTE(app, "/redfish/v1/Managers/bmc/LogServices/Journal/Entries/")
2285         .privileges(redfish::privileges::getLogEntryCollection)
2286         .methods(boost::beast::http::verb::get)(
2287             [&app](const crow::Request& req,
2288                    const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) {
2289         query_param::QueryCapabilities capabilities = {
2290             .canDelegateTop = true,
2291             .canDelegateSkip = true,
2292         };
2293         query_param::Query delegatedQuery;
2294         if (!redfish::setUpRedfishRouteWithDelegation(
2295                 app, req, asyncResp, delegatedQuery, capabilities))
2296         {
2297             return;
2298         }
2299 
2300         size_t skip = delegatedQuery.skip.value_or(0);
2301         size_t top = delegatedQuery.top.value_or(query_param::Query::maxTop);
2302 
2303         // Collections don't include the static data added by SubRoute
2304         // because it has a duplicate entry for members
2305         asyncResp->res.jsonValue["@odata.type"] =
2306             "#LogEntryCollection.LogEntryCollection";
2307         asyncResp->res.jsonValue["@odata.id"] =
2308             "/redfish/v1/Managers/bmc/LogServices/Journal/Entries";
2309         asyncResp->res.jsonValue["Name"] = "Open BMC Journal Entries";
2310         asyncResp->res.jsonValue["Description"] =
2311             "Collection of BMC Journal Entries";
2312         nlohmann::json& logEntryArray = asyncResp->res.jsonValue["Members"];
2313         logEntryArray = nlohmann::json::array();
2314 
2315         // Go through the journal and use the timestamp to create a
2316         // unique ID for each entry
2317         sd_journal* journalTmp = nullptr;
2318         int ret = sd_journal_open(&journalTmp, SD_JOURNAL_LOCAL_ONLY);
2319         if (ret < 0)
2320         {
2321             BMCWEB_LOG_ERROR << "failed to open journal: " << strerror(-ret);
2322             messages::internalError(asyncResp->res);
2323             return;
2324         }
2325         std::unique_ptr<sd_journal, decltype(&sd_journal_close)> journal(
2326             journalTmp, sd_journal_close);
2327         journalTmp = nullptr;
2328         uint64_t entryCount = 0;
2329         // Reset the unique ID on the first entry
2330         bool firstEntry = true;
2331         SD_JOURNAL_FOREACH(journal.get())
2332         {
2333             entryCount++;
2334             // Handle paging using skip (number of entries to skip from
2335             // the start) and top (number of entries to display)
2336             if (entryCount <= skip || entryCount > skip + top)
2337             {
2338                 continue;
2339             }
2340 
2341             std::string idStr;
2342             if (!getUniqueEntryID(journal.get(), idStr, firstEntry))
2343             {
2344                 continue;
2345             }
2346             firstEntry = false;
2347 
2348             nlohmann::json::object_t bmcJournalLogEntry;
2349             if (fillBMCJournalLogEntryJson(idStr, journal.get(),
2350                                            bmcJournalLogEntry) != 0)
2351             {
2352                 messages::internalError(asyncResp->res);
2353                 return;
2354             }
2355             logEntryArray.push_back(std::move(bmcJournalLogEntry));
2356         }
2357         asyncResp->res.jsonValue["Members@odata.count"] = entryCount;
2358         if (skip + top < entryCount)
2359         {
2360             asyncResp->res.jsonValue["Members@odata.nextLink"] =
2361                 "/redfish/v1/Managers/bmc/LogServices/Journal/Entries?$skip=" +
2362                 std::to_string(skip + top);
2363         }
2364         });
2365 }
2366 
2367 inline void requestRoutesBMCJournalLogEntry(App& app)
2368 {
2369     BMCWEB_ROUTE(app,
2370                  "/redfish/v1/Managers/bmc/LogServices/Journal/Entries/<str>/")
2371         .privileges(redfish::privileges::getLogEntry)
2372         .methods(boost::beast::http::verb::get)(
2373             [&app](const crow::Request& req,
2374                    const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
2375                    const std::string& entryID) {
2376         if (!redfish::setUpRedfishRoute(app, req, asyncResp))
2377         {
2378             return;
2379         }
2380         // Convert the unique ID back to a timestamp to find the entry
2381         uint64_t ts = 0;
2382         uint64_t index = 0;
2383         if (!getTimestampFromID(asyncResp, entryID, ts, index))
2384         {
2385             return;
2386         }
2387 
2388         sd_journal* journalTmp = nullptr;
2389         int ret = sd_journal_open(&journalTmp, SD_JOURNAL_LOCAL_ONLY);
2390         if (ret < 0)
2391         {
2392             BMCWEB_LOG_ERROR << "failed to open journal: " << strerror(-ret);
2393             messages::internalError(asyncResp->res);
2394             return;
2395         }
2396         std::unique_ptr<sd_journal, decltype(&sd_journal_close)> journal(
2397             journalTmp, sd_journal_close);
2398         journalTmp = nullptr;
2399         // Go to the timestamp in the log and move to the entry at the
2400         // index tracking the unique ID
2401         std::string idStr;
2402         bool firstEntry = true;
2403         ret = sd_journal_seek_realtime_usec(journal.get(), ts);
2404         if (ret < 0)
2405         {
2406             BMCWEB_LOG_ERROR << "failed to seek to an entry in journal"
2407                              << strerror(-ret);
2408             messages::internalError(asyncResp->res);
2409             return;
2410         }
2411         for (uint64_t i = 0; i <= index; i++)
2412         {
2413             sd_journal_next(journal.get());
2414             if (!getUniqueEntryID(journal.get(), idStr, firstEntry))
2415             {
2416                 messages::internalError(asyncResp->res);
2417                 return;
2418             }
2419             firstEntry = false;
2420         }
2421         // Confirm that the entry ID matches what was requested
2422         if (idStr != entryID)
2423         {
2424             messages::resourceMissingAtURI(asyncResp->res, req.urlView);
2425             return;
2426         }
2427 
2428         nlohmann::json::object_t bmcJournalLogEntry;
2429         if (fillBMCJournalLogEntryJson(entryID, journal.get(),
2430                                        bmcJournalLogEntry) != 0)
2431         {
2432             messages::internalError(asyncResp->res);
2433             return;
2434         }
2435         asyncResp->res.jsonValue.update(bmcJournalLogEntry);
2436         });
2437 }
2438 
2439 inline void
2440     getDumpServiceInfo(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
2441                        const std::string& dumpType)
2442 {
2443     std::string dumpPath;
2444     std::string overWritePolicy;
2445     bool collectDiagnosticDataSupported = false;
2446 
2447     if (dumpType == "BMC")
2448     {
2449         dumpPath = "/redfish/v1/Managers/bmc/LogServices/Dump";
2450         overWritePolicy = "WrapsWhenFull";
2451         collectDiagnosticDataSupported = true;
2452     }
2453     else if (dumpType == "FaultLog")
2454     {
2455         dumpPath = "/redfish/v1/Managers/bmc/LogServices/FaultLog";
2456         overWritePolicy = "Unknown";
2457         collectDiagnosticDataSupported = false;
2458     }
2459     else if (dumpType == "System")
2460     {
2461         dumpPath = "/redfish/v1/Systems/system/LogServices/Dump";
2462         overWritePolicy = "WrapsWhenFull";
2463         collectDiagnosticDataSupported = true;
2464     }
2465     else
2466     {
2467         BMCWEB_LOG_ERROR << "getDumpServiceInfo() invalid dump type: "
2468                          << dumpType;
2469         messages::internalError(asyncResp->res);
2470         return;
2471     }
2472 
2473     asyncResp->res.jsonValue["@odata.id"] = dumpPath;
2474     asyncResp->res.jsonValue["@odata.type"] = "#LogService.v1_2_0.LogService";
2475     asyncResp->res.jsonValue["Name"] = "Dump LogService";
2476     asyncResp->res.jsonValue["Description"] = dumpType + " Dump LogService";
2477     asyncResp->res.jsonValue["Id"] = std::filesystem::path(dumpPath).filename();
2478     asyncResp->res.jsonValue["OverWritePolicy"] = std::move(overWritePolicy);
2479 
2480     std::pair<std::string, std::string> redfishDateTimeOffset =
2481         redfish::time_utils::getDateTimeOffsetNow();
2482     asyncResp->res.jsonValue["DateTime"] = redfishDateTimeOffset.first;
2483     asyncResp->res.jsonValue["DateTimeLocalOffset"] =
2484         redfishDateTimeOffset.second;
2485 
2486     asyncResp->res.jsonValue["Entries"]["@odata.id"] = dumpPath + "/Entries";
2487     asyncResp->res.jsonValue["Actions"]["#LogService.ClearLog"]["target"] =
2488         dumpPath + "/Actions/LogService.ClearLog";
2489 
2490     if (collectDiagnosticDataSupported)
2491     {
2492         asyncResp->res.jsonValue["Actions"]["#LogService.CollectDiagnosticData"]
2493                                 ["target"] =
2494             dumpPath + "/Actions/LogService.CollectDiagnosticData";
2495     }
2496 }
2497 
2498 inline void handleLogServicesDumpServiceGet(
2499     crow::App& app, const std::string& dumpType, const crow::Request& req,
2500     const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
2501 {
2502     if (!redfish::setUpRedfishRoute(app, req, asyncResp))
2503     {
2504         return;
2505     }
2506     getDumpServiceInfo(asyncResp, dumpType);
2507 }
2508 
2509 inline void handleLogServicesDumpServiceComputerSystemGet(
2510     crow::App& app, const crow::Request& req,
2511     const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
2512     const std::string& chassisId)
2513 {
2514     if (!redfish::setUpRedfishRoute(app, req, asyncResp))
2515     {
2516         return;
2517     }
2518     if (chassisId != "system")
2519     {
2520         messages::resourceNotFound(asyncResp->res, "ComputerSystem", chassisId);
2521         return;
2522     }
2523     getDumpServiceInfo(asyncResp, "System");
2524 }
2525 
2526 inline void handleLogServicesDumpEntriesCollectionGet(
2527     crow::App& app, const std::string& dumpType, const crow::Request& req,
2528     const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
2529 {
2530     if (!redfish::setUpRedfishRoute(app, req, asyncResp))
2531     {
2532         return;
2533     }
2534     getDumpEntryCollection(asyncResp, dumpType);
2535 }
2536 
2537 inline void handleLogServicesDumpEntriesCollectionComputerSystemGet(
2538     crow::App& app, const crow::Request& req,
2539     const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
2540     const std::string& chassisId)
2541 {
2542     if (!redfish::setUpRedfishRoute(app, req, asyncResp))
2543     {
2544         return;
2545     }
2546     if (chassisId != "system")
2547     {
2548         messages::resourceNotFound(asyncResp->res, "ComputerSystem", chassisId);
2549         return;
2550     }
2551     getDumpEntryCollection(asyncResp, "System");
2552 }
2553 
2554 inline void handleLogServicesDumpEntryGet(
2555     crow::App& app, const std::string& dumpType, const crow::Request& req,
2556     const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
2557     const std::string& dumpId)
2558 {
2559     if (!redfish::setUpRedfishRoute(app, req, asyncResp))
2560     {
2561         return;
2562     }
2563     getDumpEntryById(asyncResp, dumpId, dumpType);
2564 }
2565 inline void handleLogServicesDumpEntryComputerSystemGet(
2566     crow::App& app, const crow::Request& req,
2567     const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
2568     const std::string& chassisId, const std::string& dumpId)
2569 {
2570     if (!redfish::setUpRedfishRoute(app, req, asyncResp))
2571     {
2572         return;
2573     }
2574     if (chassisId != "system")
2575     {
2576         messages::resourceNotFound(asyncResp->res, "ComputerSystem", chassisId);
2577         return;
2578     }
2579     getDumpEntryById(asyncResp, dumpId, "System");
2580 }
2581 
2582 inline void handleLogServicesDumpEntryDelete(
2583     crow::App& app, const std::string& dumpType, const crow::Request& req,
2584     const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
2585     const std::string& dumpId)
2586 {
2587     if (!redfish::setUpRedfishRoute(app, req, asyncResp))
2588     {
2589         return;
2590     }
2591     deleteDumpEntry(asyncResp, dumpId, dumpType);
2592 }
2593 
2594 inline void handleLogServicesDumpEntryComputerSystemDelete(
2595     crow::App& app, const crow::Request& req,
2596     const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
2597     const std::string& chassisId, const std::string& dumpId)
2598 {
2599     if (!redfish::setUpRedfishRoute(app, req, asyncResp))
2600     {
2601         return;
2602     }
2603     if (chassisId != "system")
2604     {
2605         messages::resourceNotFound(asyncResp->res, "ComputerSystem", chassisId);
2606         return;
2607     }
2608     deleteDumpEntry(asyncResp, dumpId, "System");
2609 }
2610 
2611 inline void handleLogServicesDumpCollectDiagnosticDataPost(
2612     crow::App& app, const std::string& dumpType, const crow::Request& req,
2613     const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
2614 {
2615     if (!redfish::setUpRedfishRoute(app, req, asyncResp))
2616     {
2617         return;
2618     }
2619     createDump(asyncResp, req, dumpType);
2620 }
2621 
2622 inline void handleLogServicesDumpCollectDiagnosticDataComputerSystemPost(
2623     crow::App& app, const crow::Request& req,
2624     const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
2625     const std::string& chassisId)
2626 {
2627     if (!redfish::setUpRedfishRoute(app, req, asyncResp))
2628     {
2629         return;
2630     }
2631     if (chassisId != "system")
2632     {
2633         messages::resourceNotFound(asyncResp->res, "ComputerSystem", chassisId);
2634         return;
2635     }
2636     createDump(asyncResp, req, "System");
2637 }
2638 
2639 inline void handleLogServicesDumpClearLogPost(
2640     crow::App& app, const std::string& dumpType, const crow::Request& req,
2641     const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
2642 {
2643     if (!redfish::setUpRedfishRoute(app, req, asyncResp))
2644     {
2645         return;
2646     }
2647     clearDump(asyncResp, dumpType);
2648 }
2649 
2650 inline void handleLogServicesDumpClearLogComputerSystemPost(
2651     crow::App& app, const crow::Request& req,
2652     const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
2653     const std::string& chassisId)
2654 {
2655     if (!redfish::setUpRedfishRoute(app, req, asyncResp))
2656     {
2657         return;
2658     }
2659     if (chassisId != "system")
2660     {
2661         messages::resourceNotFound(asyncResp->res, "ComputerSystem", chassisId);
2662         return;
2663     }
2664     clearDump(asyncResp, "System");
2665 }
2666 
2667 inline void requestRoutesBMCDumpService(App& app)
2668 {
2669     BMCWEB_ROUTE(app, "/redfish/v1/Managers/bmc/LogServices/Dump/")
2670         .privileges(redfish::privileges::getLogService)
2671         .methods(boost::beast::http::verb::get)(std::bind_front(
2672             handleLogServicesDumpServiceGet, std::ref(app), "BMC"));
2673 }
2674 
2675 inline void requestRoutesBMCDumpEntryCollection(App& app)
2676 {
2677     BMCWEB_ROUTE(app, "/redfish/v1/Managers/bmc/LogServices/Dump/Entries/")
2678         .privileges(redfish::privileges::getLogEntryCollection)
2679         .methods(boost::beast::http::verb::get)(std::bind_front(
2680             handleLogServicesDumpEntriesCollectionGet, std::ref(app), "BMC"));
2681 }
2682 
2683 inline void requestRoutesBMCDumpEntry(App& app)
2684 {
2685     BMCWEB_ROUTE(app,
2686                  "/redfish/v1/Managers/bmc/LogServices/Dump/Entries/<str>/")
2687         .privileges(redfish::privileges::getLogEntry)
2688         .methods(boost::beast::http::verb::get)(std::bind_front(
2689             handleLogServicesDumpEntryGet, std::ref(app), "BMC"));
2690 
2691     BMCWEB_ROUTE(app,
2692                  "/redfish/v1/Managers/bmc/LogServices/Dump/Entries/<str>/")
2693         .privileges(redfish::privileges::deleteLogEntry)
2694         .methods(boost::beast::http::verb::delete_)(std::bind_front(
2695             handleLogServicesDumpEntryDelete, std::ref(app), "BMC"));
2696 }
2697 
2698 inline void requestRoutesBMCDumpCreate(App& app)
2699 {
2700     BMCWEB_ROUTE(
2701         app,
2702         "/redfish/v1/Managers/bmc/LogServices/Dump/Actions/LogService.CollectDiagnosticData/")
2703         .privileges(redfish::privileges::postLogService)
2704         .methods(boost::beast::http::verb::post)(
2705             std::bind_front(handleLogServicesDumpCollectDiagnosticDataPost,
2706                             std::ref(app), "BMC"));
2707 }
2708 
2709 inline void requestRoutesBMCDumpClear(App& app)
2710 {
2711     BMCWEB_ROUTE(
2712         app,
2713         "/redfish/v1/Managers/bmc/LogServices/Dump/Actions/LogService.ClearLog/")
2714         .privileges(redfish::privileges::postLogService)
2715         .methods(boost::beast::http::verb::post)(std::bind_front(
2716             handleLogServicesDumpClearLogPost, std::ref(app), "BMC"));
2717 }
2718 
2719 inline void requestRoutesFaultLogDumpService(App& app)
2720 {
2721     BMCWEB_ROUTE(app, "/redfish/v1/Managers/bmc/LogServices/FaultLog/")
2722         .privileges(redfish::privileges::getLogService)
2723         .methods(boost::beast::http::verb::get)(std::bind_front(
2724             handleLogServicesDumpServiceGet, std::ref(app), "FaultLog"));
2725 }
2726 
2727 inline void requestRoutesFaultLogDumpEntryCollection(App& app)
2728 {
2729     BMCWEB_ROUTE(app, "/redfish/v1/Managers/bmc/LogServices/FaultLog/Entries/")
2730         .privileges(redfish::privileges::getLogEntryCollection)
2731         .methods(boost::beast::http::verb::get)(
2732             std::bind_front(handleLogServicesDumpEntriesCollectionGet,
2733                             std::ref(app), "FaultLog"));
2734 }
2735 
2736 inline void requestRoutesFaultLogDumpEntry(App& app)
2737 {
2738     BMCWEB_ROUTE(app,
2739                  "/redfish/v1/Managers/bmc/LogServices/FaultLog/Entries/<str>/")
2740         .privileges(redfish::privileges::getLogEntry)
2741         .methods(boost::beast::http::verb::get)(std::bind_front(
2742             handleLogServicesDumpEntryGet, std::ref(app), "FaultLog"));
2743 
2744     BMCWEB_ROUTE(app,
2745                  "/redfish/v1/Managers/bmc/LogServices/FaultLog/Entries/<str>/")
2746         .privileges(redfish::privileges::deleteLogEntry)
2747         .methods(boost::beast::http::verb::delete_)(std::bind_front(
2748             handleLogServicesDumpEntryDelete, std::ref(app), "FaultLog"));
2749 }
2750 
2751 inline void requestRoutesFaultLogDumpClear(App& app)
2752 {
2753     BMCWEB_ROUTE(
2754         app,
2755         "/redfish/v1/Managers/bmc/LogServices/FaultLog/Actions/LogService.ClearLog/")
2756         .privileges(redfish::privileges::postLogService)
2757         .methods(boost::beast::http::verb::post)(std::bind_front(
2758             handleLogServicesDumpClearLogPost, std::ref(app), "FaultLog"));
2759 }
2760 
2761 inline void requestRoutesSystemDumpService(App& app)
2762 {
2763     BMCWEB_ROUTE(app, "/redfish/v1/Systems/<str>/LogServices/Dump/")
2764         .privileges(redfish::privileges::getLogService)
2765         .methods(boost::beast::http::verb::get)(std::bind_front(
2766             handleLogServicesDumpServiceComputerSystemGet, std::ref(app)));
2767 }
2768 
2769 inline void requestRoutesSystemDumpEntryCollection(App& app)
2770 {
2771     BMCWEB_ROUTE(app, "/redfish/v1/Systems/<str>/LogServices/Dump/Entries/")
2772         .privileges(redfish::privileges::getLogEntryCollection)
2773         .methods(boost::beast::http::verb::get)(std::bind_front(
2774             handleLogServicesDumpEntriesCollectionComputerSystemGet,
2775             std::ref(app)));
2776 }
2777 
2778 inline void requestRoutesSystemDumpEntry(App& app)
2779 {
2780     BMCWEB_ROUTE(app,
2781                  "/redfish/v1/Systems/<str>/LogServices/Dump/Entries/<str>/")
2782         .privileges(redfish::privileges::getLogEntry)
2783         .methods(boost::beast::http::verb::get)(std::bind_front(
2784             handleLogServicesDumpEntryComputerSystemGet, std::ref(app)));
2785 
2786     BMCWEB_ROUTE(app,
2787                  "/redfish/v1/Systems/<str>/LogServices/Dump/Entries/<str>/")
2788         .privileges(redfish::privileges::deleteLogEntry)
2789         .methods(boost::beast::http::verb::delete_)(std::bind_front(
2790             handleLogServicesDumpEntryComputerSystemDelete, std::ref(app)));
2791 }
2792 
2793 inline void requestRoutesSystemDumpCreate(App& app)
2794 {
2795     BMCWEB_ROUTE(
2796         app,
2797         "/redfish/v1/Systems/<str>/LogServices/Dump/Actions/LogService.CollectDiagnosticData/")
2798         .privileges(redfish::privileges::postLogService)
2799         .methods(boost::beast::http::verb::post)(std::bind_front(
2800             handleLogServicesDumpCollectDiagnosticDataComputerSystemPost,
2801             std::ref(app)));
2802 }
2803 
2804 inline void requestRoutesSystemDumpClear(App& app)
2805 {
2806     BMCWEB_ROUTE(
2807         app,
2808         "/redfish/v1/Systems/<str>/LogServices/Dump/Actions/LogService.ClearLog/")
2809         .privileges(redfish::privileges::postLogService)
2810         .methods(boost::beast::http::verb::post)(std::bind_front(
2811             handleLogServicesDumpClearLogComputerSystemPost, std::ref(app)));
2812 }
2813 
2814 inline void requestRoutesCrashdumpService(App& app)
2815 {
2816     // Note: Deviated from redfish privilege registry for GET & HEAD
2817     // method for security reasons.
2818     /**
2819      * Functions triggers appropriate requests on DBus
2820      */
2821     BMCWEB_ROUTE(app, "/redfish/v1/Systems/<str>/LogServices/Crashdump/")
2822         // This is incorrect, should be:
2823         //.privileges(redfish::privileges::getLogService)
2824         .privileges({{"ConfigureManager"}})
2825         .methods(boost::beast::http::verb::get)(
2826             [&app](const crow::Request& req,
2827                    const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
2828                    const std::string& systemName) {
2829         if (!redfish::setUpRedfishRoute(app, req, asyncResp))
2830         {
2831             return;
2832         }
2833         if (systemName != "system")
2834         {
2835             messages::resourceNotFound(asyncResp->res, "ComputerSystem",
2836                                        systemName);
2837             return;
2838         }
2839 
2840         // Copy over the static data to include the entries added by
2841         // SubRoute
2842         asyncResp->res.jsonValue["@odata.id"] =
2843             "/redfish/v1/Systems/system/LogServices/Crashdump";
2844         asyncResp->res.jsonValue["@odata.type"] =
2845             "#LogService.v1_2_0.LogService";
2846         asyncResp->res.jsonValue["Name"] = "Open BMC Oem Crashdump Service";
2847         asyncResp->res.jsonValue["Description"] = "Oem Crashdump Service";
2848         asyncResp->res.jsonValue["Id"] = "Oem Crashdump";
2849         asyncResp->res.jsonValue["OverWritePolicy"] = "WrapsWhenFull";
2850         asyncResp->res.jsonValue["MaxNumberOfRecords"] = 3;
2851 
2852         std::pair<std::string, std::string> redfishDateTimeOffset =
2853             redfish::time_utils::getDateTimeOffsetNow();
2854         asyncResp->res.jsonValue["DateTime"] = redfishDateTimeOffset.first;
2855         asyncResp->res.jsonValue["DateTimeLocalOffset"] =
2856             redfishDateTimeOffset.second;
2857 
2858         asyncResp->res.jsonValue["Entries"]["@odata.id"] =
2859             "/redfish/v1/Systems/system/LogServices/Crashdump/Entries";
2860         asyncResp->res.jsonValue["Actions"]["#LogService.ClearLog"]["target"] =
2861             "/redfish/v1/Systems/system/LogServices/Crashdump/Actions/LogService.ClearLog";
2862         asyncResp->res.jsonValue["Actions"]["#LogService.CollectDiagnosticData"]
2863                                 ["target"] =
2864             "/redfish/v1/Systems/system/LogServices/Crashdump/Actions/LogService.CollectDiagnosticData";
2865         });
2866 }
2867 
2868 void inline requestRoutesCrashdumpClear(App& app)
2869 {
2870     BMCWEB_ROUTE(
2871         app,
2872         "/redfish/v1/Systems/<str>/LogServices/Crashdump/Actions/LogService.ClearLog/")
2873         // This is incorrect, should be:
2874         //.privileges(redfish::privileges::postLogService)
2875         .privileges({{"ConfigureComponents"}})
2876         .methods(boost::beast::http::verb::post)(
2877             [&app](const crow::Request& req,
2878                    const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
2879                    const std::string& systemName) {
2880         if (!redfish::setUpRedfishRoute(app, req, asyncResp))
2881         {
2882             return;
2883         }
2884         if (systemName != "system")
2885         {
2886             messages::resourceNotFound(asyncResp->res, "ComputerSystem",
2887                                        systemName);
2888             return;
2889         }
2890         crow::connections::systemBus->async_method_call(
2891             [asyncResp](const boost::system::error_code ec,
2892                         const std::string&) {
2893             if (ec)
2894             {
2895                 messages::internalError(asyncResp->res);
2896                 return;
2897             }
2898             messages::success(asyncResp->res);
2899             },
2900             crashdumpObject, crashdumpPath, deleteAllInterface, "DeleteAll");
2901         });
2902 }
2903 
2904 static void
2905     logCrashdumpEntry(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
2906                       const std::string& logID, nlohmann::json& logEntryJson)
2907 {
2908     auto getStoredLogCallback =
2909         [asyncResp, logID,
2910          &logEntryJson](const boost::system::error_code ec,
2911                         const dbus::utility::DBusPropertiesMap& params) {
2912         if (ec)
2913         {
2914             BMCWEB_LOG_DEBUG << "failed to get log ec: " << ec.message();
2915             if (ec.value() ==
2916                 boost::system::linux_error::bad_request_descriptor)
2917             {
2918                 messages::resourceNotFound(asyncResp->res, "LogEntry", logID);
2919             }
2920             else
2921             {
2922                 messages::internalError(asyncResp->res);
2923             }
2924             return;
2925         }
2926 
2927         std::string timestamp{};
2928         std::string filename{};
2929         std::string logfile{};
2930         parseCrashdumpParameters(params, filename, timestamp, logfile);
2931 
2932         if (filename.empty() || timestamp.empty())
2933         {
2934             messages::resourceMissingAtURI(asyncResp->res,
2935                                            crow::utility::urlFromPieces(logID));
2936             return;
2937         }
2938 
2939         std::string crashdumpURI =
2940             "/redfish/v1/Systems/system/LogServices/Crashdump/Entries/" +
2941             logID + "/" + filename;
2942         nlohmann::json::object_t logEntry;
2943         logEntry["@odata.type"] = "#LogEntry.v1_9_0.LogEntry";
2944         logEntry["@odata.id"] =
2945             "/redfish/v1/Systems/system/LogServices/Crashdump/Entries/" + logID;
2946         logEntry["Name"] = "CPU Crashdump";
2947         logEntry["Id"] = logID;
2948         logEntry["EntryType"] = "Oem";
2949         logEntry["AdditionalDataURI"] = std::move(crashdumpURI);
2950         logEntry["DiagnosticDataType"] = "OEM";
2951         logEntry["OEMDiagnosticDataType"] = "PECICrashdump";
2952         logEntry["Created"] = std::move(timestamp);
2953 
2954         // If logEntryJson references an array of LogEntry resources
2955         // ('Members' list), then push this as a new entry, otherwise set it
2956         // directly
2957         if (logEntryJson.is_array())
2958         {
2959             logEntryJson.push_back(logEntry);
2960             asyncResp->res.jsonValue["Members@odata.count"] =
2961                 logEntryJson.size();
2962         }
2963         else
2964         {
2965             logEntryJson.update(logEntry);
2966         }
2967     };
2968     sdbusplus::asio::getAllProperties(
2969         *crow::connections::systemBus, crashdumpObject,
2970         crashdumpPath + std::string("/") + logID, crashdumpInterface,
2971         std::move(getStoredLogCallback));
2972 }
2973 
2974 inline void requestRoutesCrashdumpEntryCollection(App& app)
2975 {
2976     // Note: Deviated from redfish privilege registry for GET & HEAD
2977     // method for security reasons.
2978     /**
2979      * Functions triggers appropriate requests on DBus
2980      */
2981     BMCWEB_ROUTE(app,
2982                  "/redfish/v1/Systems/<str>/LogServices/Crashdump/Entries/")
2983         // This is incorrect, should be.
2984         //.privileges(redfish::privileges::postLogEntryCollection)
2985         .privileges({{"ConfigureComponents"}})
2986         .methods(boost::beast::http::verb::get)(
2987             [&app](const crow::Request& req,
2988                    const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
2989                    const std::string& systemName) {
2990         if (!redfish::setUpRedfishRoute(app, req, asyncResp))
2991         {
2992             return;
2993         }
2994         if (systemName != "system")
2995         {
2996             messages::resourceNotFound(asyncResp->res, "ComputerSystem",
2997                                        systemName);
2998             return;
2999         }
3000 
3001         crow::connections::systemBus->async_method_call(
3002             [asyncResp](const boost::system::error_code ec,
3003                         const std::vector<std::string>& resp) {
3004             if (ec)
3005             {
3006                 if (ec.value() !=
3007                     boost::system::errc::no_such_file_or_directory)
3008                 {
3009                     BMCWEB_LOG_DEBUG << "failed to get entries ec: "
3010                                      << ec.message();
3011                     messages::internalError(asyncResp->res);
3012                     return;
3013                 }
3014             }
3015             asyncResp->res.jsonValue["@odata.type"] =
3016                 "#LogEntryCollection.LogEntryCollection";
3017             asyncResp->res.jsonValue["@odata.id"] =
3018                 "/redfish/v1/Systems/system/LogServices/Crashdump/Entries";
3019             asyncResp->res.jsonValue["Name"] = "Open BMC Crashdump Entries";
3020             asyncResp->res.jsonValue["Description"] =
3021                 "Collection of Crashdump Entries";
3022             asyncResp->res.jsonValue["Members"] = nlohmann::json::array();
3023             asyncResp->res.jsonValue["Members@odata.count"] = 0;
3024 
3025             for (const std::string& path : resp)
3026             {
3027                 const sdbusplus::message::object_path objPath(path);
3028                 // Get the log ID
3029                 std::string logID = objPath.filename();
3030                 if (logID.empty())
3031                 {
3032                     continue;
3033                 }
3034                 // Add the log entry to the array
3035                 logCrashdumpEntry(asyncResp, logID,
3036                                   asyncResp->res.jsonValue["Members"]);
3037             }
3038             },
3039             "xyz.openbmc_project.ObjectMapper",
3040             "/xyz/openbmc_project/object_mapper",
3041             "xyz.openbmc_project.ObjectMapper", "GetSubTreePaths", "", 0,
3042             std::array<const char*, 1>{crashdumpInterface});
3043         });
3044 }
3045 
3046 inline void requestRoutesCrashdumpEntry(App& app)
3047 {
3048     // Note: Deviated from redfish privilege registry for GET & HEAD
3049     // method for security reasons.
3050 
3051     BMCWEB_ROUTE(
3052         app, "/redfish/v1/Systems/<str>/LogServices/Crashdump/Entries/<str>/")
3053         // this is incorrect, should be
3054         // .privileges(redfish::privileges::getLogEntry)
3055         .privileges({{"ConfigureComponents"}})
3056         .methods(boost::beast::http::verb::get)(
3057             [&app](const crow::Request& req,
3058                    const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
3059                    const std::string& systemName, const std::string& param) {
3060         if (!redfish::setUpRedfishRoute(app, req, asyncResp))
3061         {
3062             return;
3063         }
3064         if (systemName != "system")
3065         {
3066             messages::resourceNotFound(asyncResp->res, "ComputerSystem",
3067                                        systemName);
3068             ;
3069             return;
3070         }
3071         const std::string& logID = param;
3072         logCrashdumpEntry(asyncResp, logID, asyncResp->res.jsonValue);
3073         });
3074 }
3075 
3076 inline void requestRoutesCrashdumpFile(App& app)
3077 {
3078     // Note: Deviated from redfish privilege registry for GET & HEAD
3079     // method for security reasons.
3080     BMCWEB_ROUTE(
3081         app,
3082         "/redfish/v1/Systems/<str>/LogServices/Crashdump/Entries/<str>/<str>/")
3083         .privileges(redfish::privileges::getLogEntry)
3084         .methods(boost::beast::http::verb::get)(
3085             [](const crow::Request& req,
3086                const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
3087                const std::string& systemName, const std::string& logID,
3088                const std::string& fileName) {
3089         // Do not call getRedfishRoute here since the crashdump file is not a
3090         // Redfish resource.
3091 
3092         if (systemName != "system")
3093         {
3094             messages::resourceNotFound(asyncResp->res, "ComputerSystem",
3095                                        systemName);
3096             ;
3097             return;
3098         }
3099 
3100         auto getStoredLogCallback =
3101             [asyncResp, logID, fileName, url(boost::urls::url(req.urlView))](
3102                 const boost::system::error_code ec,
3103                 const std::vector<
3104                     std::pair<std::string, dbus::utility::DbusVariantType>>&
3105                     resp) {
3106             if (ec)
3107             {
3108                 BMCWEB_LOG_DEBUG << "failed to get log ec: " << ec.message();
3109                 messages::internalError(asyncResp->res);
3110                 return;
3111             }
3112 
3113             std::string dbusFilename{};
3114             std::string dbusTimestamp{};
3115             std::string dbusFilepath{};
3116 
3117             parseCrashdumpParameters(resp, dbusFilename, dbusTimestamp,
3118                                      dbusFilepath);
3119 
3120             if (dbusFilename.empty() || dbusTimestamp.empty() ||
3121                 dbusFilepath.empty())
3122             {
3123                 messages::resourceMissingAtURI(asyncResp->res, url);
3124                 return;
3125             }
3126 
3127             // Verify the file name parameter is correct
3128             if (fileName != dbusFilename)
3129             {
3130                 messages::resourceMissingAtURI(asyncResp->res, url);
3131                 return;
3132             }
3133 
3134             if (!std::filesystem::exists(dbusFilepath))
3135             {
3136                 messages::resourceMissingAtURI(asyncResp->res, url);
3137                 return;
3138             }
3139             std::ifstream ifs(dbusFilepath, std::ios::in | std::ios::binary);
3140             asyncResp->res.body() =
3141                 std::string(std::istreambuf_iterator<char>{ifs}, {});
3142 
3143             // Configure this to be a file download when accessed
3144             // from a browser
3145             asyncResp->res.addHeader(
3146                 boost::beast::http::field::content_disposition, "attachment");
3147         };
3148         sdbusplus::asio::getAllProperties(
3149             *crow::connections::systemBus, crashdumpObject,
3150             crashdumpPath + std::string("/") + logID, crashdumpInterface,
3151             std::move(getStoredLogCallback));
3152         });
3153 }
3154 
3155 enum class OEMDiagnosticType
3156 {
3157     onDemand,
3158     telemetry,
3159     invalid,
3160 };
3161 
3162 inline OEMDiagnosticType
3163     getOEMDiagnosticType(const std::string_view& oemDiagStr)
3164 {
3165     if (oemDiagStr == "OnDemand")
3166     {
3167         return OEMDiagnosticType::onDemand;
3168     }
3169     if (oemDiagStr == "Telemetry")
3170     {
3171         return OEMDiagnosticType::telemetry;
3172     }
3173 
3174     return OEMDiagnosticType::invalid;
3175 }
3176 
3177 inline void requestRoutesCrashdumpCollect(App& app)
3178 {
3179     // Note: Deviated from redfish privilege registry for GET & HEAD
3180     // method for security reasons.
3181     BMCWEB_ROUTE(
3182         app,
3183         "/redfish/v1/Systems/<str>/LogServices/Crashdump/Actions/LogService.CollectDiagnosticData/")
3184         // The below is incorrect;  Should be ConfigureManager
3185         //.privileges(redfish::privileges::postLogService)
3186         .privileges({{"ConfigureComponents"}})
3187         .methods(boost::beast::http::verb::post)(
3188             [&app](const crow::Request& req,
3189                    const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
3190                    const std::string& systemName) {
3191         if (!redfish::setUpRedfishRoute(app, req, asyncResp))
3192         {
3193             return;
3194         }
3195 
3196         if (systemName != "system")
3197         {
3198             messages::resourceNotFound(asyncResp->res, "ComputerSystem",
3199                                        systemName);
3200             ;
3201             return;
3202         }
3203 
3204         std::string diagnosticDataType;
3205         std::string oemDiagnosticDataType;
3206         if (!redfish::json_util::readJsonAction(
3207                 req, asyncResp->res, "DiagnosticDataType", diagnosticDataType,
3208                 "OEMDiagnosticDataType", oemDiagnosticDataType))
3209         {
3210             return;
3211         }
3212 
3213         if (diagnosticDataType != "OEM")
3214         {
3215             BMCWEB_LOG_ERROR
3216                 << "Only OEM DiagnosticDataType supported for Crashdump";
3217             messages::actionParameterValueFormatError(
3218                 asyncResp->res, diagnosticDataType, "DiagnosticDataType",
3219                 "CollectDiagnosticData");
3220             return;
3221         }
3222 
3223         OEMDiagnosticType oemDiagType =
3224             getOEMDiagnosticType(oemDiagnosticDataType);
3225 
3226         std::string iface;
3227         std::string method;
3228         std::string taskMatchStr;
3229         if (oemDiagType == OEMDiagnosticType::onDemand)
3230         {
3231             iface = crashdumpOnDemandInterface;
3232             method = "GenerateOnDemandLog";
3233             taskMatchStr = "type='signal',"
3234                            "interface='org.freedesktop.DBus.Properties',"
3235                            "member='PropertiesChanged',"
3236                            "arg0namespace='com.intel.crashdump'";
3237         }
3238         else if (oemDiagType == OEMDiagnosticType::telemetry)
3239         {
3240             iface = crashdumpTelemetryInterface;
3241             method = "GenerateTelemetryLog";
3242             taskMatchStr = "type='signal',"
3243                            "interface='org.freedesktop.DBus.Properties',"
3244                            "member='PropertiesChanged',"
3245                            "arg0namespace='com.intel.crashdump'";
3246         }
3247         else
3248         {
3249             BMCWEB_LOG_ERROR << "Unsupported OEMDiagnosticDataType: "
3250                              << oemDiagnosticDataType;
3251             messages::actionParameterValueFormatError(
3252                 asyncResp->res, oemDiagnosticDataType, "OEMDiagnosticDataType",
3253                 "CollectDiagnosticData");
3254             return;
3255         }
3256 
3257         auto collectCrashdumpCallback =
3258             [asyncResp, payload(task::Payload(req)),
3259              taskMatchStr](const boost::system::error_code ec,
3260                            const std::string&) mutable {
3261             if (ec)
3262             {
3263                 if (ec.value() == boost::system::errc::operation_not_supported)
3264                 {
3265                     messages::resourceInStandby(asyncResp->res);
3266                 }
3267                 else if (ec.value() ==
3268                          boost::system::errc::device_or_resource_busy)
3269                 {
3270                     messages::serviceTemporarilyUnavailable(asyncResp->res,
3271                                                             "60");
3272                 }
3273                 else
3274                 {
3275                     messages::internalError(asyncResp->res);
3276                 }
3277                 return;
3278             }
3279             std::shared_ptr<task::TaskData> task = task::TaskData::createTask(
3280                 [](boost::system::error_code err, sdbusplus::message_t&,
3281                    const std::shared_ptr<task::TaskData>& taskData) {
3282                 if (!err)
3283                 {
3284                     taskData->messages.emplace_back(messages::taskCompletedOK(
3285                         std::to_string(taskData->index)));
3286                     taskData->state = "Completed";
3287                 }
3288                 return task::completed;
3289                 },
3290                 taskMatchStr);
3291 
3292             task->startTimer(std::chrono::minutes(5));
3293             task->populateResp(asyncResp->res);
3294             task->payload.emplace(std::move(payload));
3295         };
3296 
3297         crow::connections::systemBus->async_method_call(
3298             std::move(collectCrashdumpCallback), crashdumpObject, crashdumpPath,
3299             iface, method);
3300         });
3301 }
3302 
3303 /**
3304  * DBusLogServiceActionsClear class supports POST method for ClearLog action.
3305  */
3306 inline void requestRoutesDBusLogServiceActionsClear(App& app)
3307 {
3308     /**
3309      * Function handles POST method request.
3310      * The Clear Log actions does not require any parameter.The action deletes
3311      * all entries found in the Entries collection for this Log Service.
3312      */
3313 
3314     BMCWEB_ROUTE(
3315         app,
3316         "/redfish/v1/Systems/<str>/LogServices/EventLog/Actions/LogService.ClearLog/")
3317         .privileges(redfish::privileges::postLogService)
3318         .methods(boost::beast::http::verb::post)(
3319             [&app](const crow::Request& req,
3320                    const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
3321                    const std::string& systemName) {
3322         if (!redfish::setUpRedfishRoute(app, req, asyncResp))
3323         {
3324             return;
3325         }
3326         if (systemName != "system")
3327         {
3328             messages::resourceNotFound(asyncResp->res, "ComputerSystem",
3329                                        systemName);
3330             ;
3331             return;
3332         }
3333         BMCWEB_LOG_DEBUG << "Do delete all entries.";
3334 
3335         // Process response from Logging service.
3336         auto respHandler = [asyncResp](const boost::system::error_code ec) {
3337             BMCWEB_LOG_DEBUG << "doClearLog resp_handler callback: Done";
3338             if (ec)
3339             {
3340                 // TODO Handle for specific error code
3341                 BMCWEB_LOG_ERROR << "doClearLog resp_handler got error " << ec;
3342                 asyncResp->res.result(
3343                     boost::beast::http::status::internal_server_error);
3344                 return;
3345             }
3346 
3347             asyncResp->res.result(boost::beast::http::status::no_content);
3348         };
3349 
3350         // Make call to Logging service to request Clear Log
3351         crow::connections::systemBus->async_method_call(
3352             respHandler, "xyz.openbmc_project.Logging",
3353             "/xyz/openbmc_project/logging",
3354             "xyz.openbmc_project.Collection.DeleteAll", "DeleteAll");
3355         });
3356 }
3357 
3358 /****************************************************
3359  * Redfish PostCode interfaces
3360  * using DBUS interface: getPostCodesTS
3361  ******************************************************/
3362 inline void requestRoutesPostCodesLogService(App& app)
3363 {
3364     BMCWEB_ROUTE(app, "/redfish/v1/Systems/<str>/LogServices/PostCodes/")
3365         .privileges(redfish::privileges::getLogService)
3366         .methods(boost::beast::http::verb::get)(
3367             [&app](const crow::Request& req,
3368                    const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
3369                    const std::string& systemName) {
3370         if (!redfish::setUpRedfishRoute(app, req, asyncResp))
3371         {
3372             return;
3373         }
3374         if (systemName != "system")
3375         {
3376             messages::resourceNotFound(asyncResp->res, "ComputerSystem",
3377                                        systemName);
3378             ;
3379             return;
3380         }
3381         asyncResp->res.jsonValue["@odata.id"] =
3382             "/redfish/v1/Systems/system/LogServices/PostCodes";
3383         asyncResp->res.jsonValue["@odata.type"] =
3384             "#LogService.v1_1_0.LogService";
3385         asyncResp->res.jsonValue["Name"] = "POST Code Log Service";
3386         asyncResp->res.jsonValue["Description"] = "POST Code Log Service";
3387         asyncResp->res.jsonValue["Id"] = "BIOS POST Code Log";
3388         asyncResp->res.jsonValue["OverWritePolicy"] = "WrapsWhenFull";
3389         asyncResp->res.jsonValue["Entries"]["@odata.id"] =
3390             "/redfish/v1/Systems/system/LogServices/PostCodes/Entries";
3391 
3392         std::pair<std::string, std::string> redfishDateTimeOffset =
3393             redfish::time_utils::getDateTimeOffsetNow();
3394         asyncResp->res.jsonValue["DateTime"] = redfishDateTimeOffset.first;
3395         asyncResp->res.jsonValue["DateTimeLocalOffset"] =
3396             redfishDateTimeOffset.second;
3397 
3398         asyncResp->res.jsonValue["Actions"]["#LogService.ClearLog"] = {
3399             {"target",
3400              "/redfish/v1/Systems/system/LogServices/PostCodes/Actions/LogService.ClearLog"}};
3401         });
3402 }
3403 
3404 inline void requestRoutesPostCodesClear(App& app)
3405 {
3406     BMCWEB_ROUTE(
3407         app,
3408         "/redfish/v1/Systems/<str>/LogServices/PostCodes/Actions/LogService.ClearLog/")
3409         // The following privilege is incorrect;  It should be ConfigureManager
3410         //.privileges(redfish::privileges::postLogService)
3411         .privileges({{"ConfigureComponents"}})
3412         .methods(boost::beast::http::verb::post)(
3413             [&app](const crow::Request& req,
3414                    const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
3415                    const std::string& systemName) {
3416         if (!redfish::setUpRedfishRoute(app, req, asyncResp))
3417         {
3418             return;
3419         }
3420         if (systemName != "system")
3421         {
3422             messages::resourceNotFound(asyncResp->res, "ComputerSystem",
3423                                        systemName);
3424             ;
3425             return;
3426         }
3427         BMCWEB_LOG_DEBUG << "Do delete all postcodes entries.";
3428 
3429         // Make call to post-code service to request clear all
3430         crow::connections::systemBus->async_method_call(
3431             [asyncResp](const boost::system::error_code ec) {
3432             if (ec)
3433             {
3434                 // TODO Handle for specific error code
3435                 BMCWEB_LOG_ERROR << "doClearPostCodes resp_handler got error "
3436                                  << ec;
3437                 asyncResp->res.result(
3438                     boost::beast::http::status::internal_server_error);
3439                 messages::internalError(asyncResp->res);
3440                 return;
3441             }
3442             },
3443             "xyz.openbmc_project.State.Boot.PostCode0",
3444             "/xyz/openbmc_project/State/Boot/PostCode0",
3445             "xyz.openbmc_project.Collection.DeleteAll", "DeleteAll");
3446         });
3447 }
3448 
3449 static void fillPostCodeEntry(
3450     const std::shared_ptr<bmcweb::AsyncResp>& aResp,
3451     const boost::container::flat_map<
3452         uint64_t, std::tuple<uint64_t, std::vector<uint8_t>>>& postcode,
3453     const uint16_t bootIndex, const uint64_t codeIndex = 0,
3454     const uint64_t skip = 0, const uint64_t top = 0)
3455 {
3456     // Get the Message from the MessageRegistry
3457     const registries::Message* message =
3458         registries::getMessage("OpenBMC.0.2.BIOSPOSTCode");
3459 
3460     uint64_t currentCodeIndex = 0;
3461     nlohmann::json& logEntryArray = aResp->res.jsonValue["Members"];
3462 
3463     uint64_t firstCodeTimeUs = 0;
3464     for (const std::pair<uint64_t, std::tuple<uint64_t, std::vector<uint8_t>>>&
3465              code : postcode)
3466     {
3467         currentCodeIndex++;
3468         std::string postcodeEntryID =
3469             "B" + std::to_string(bootIndex) + "-" +
3470             std::to_string(currentCodeIndex); // 1 based index in EntryID string
3471 
3472         uint64_t usecSinceEpoch = code.first;
3473         uint64_t usTimeOffset = 0;
3474 
3475         if (1 == currentCodeIndex)
3476         { // already incremented
3477             firstCodeTimeUs = code.first;
3478         }
3479         else
3480         {
3481             usTimeOffset = code.first - firstCodeTimeUs;
3482         }
3483 
3484         // skip if no specific codeIndex is specified and currentCodeIndex does
3485         // not fall between top and skip
3486         if ((codeIndex == 0) &&
3487             (currentCodeIndex <= skip || currentCodeIndex > top))
3488         {
3489             continue;
3490         }
3491 
3492         // skip if a specific codeIndex is specified and does not match the
3493         // currentIndex
3494         if ((codeIndex > 0) && (currentCodeIndex != codeIndex))
3495         {
3496             // This is done for simplicity. 1st entry is needed to calculate
3497             // time offset. To improve efficiency, one can get to the entry
3498             // directly (possibly with flatmap's nth method)
3499             continue;
3500         }
3501 
3502         // currentCodeIndex is within top and skip or equal to specified code
3503         // index
3504 
3505         // Get the Created time from the timestamp
3506         std::string entryTimeStr;
3507         entryTimeStr =
3508             redfish::time_utils::getDateTimeUint(usecSinceEpoch / 1000 / 1000);
3509 
3510         // assemble messageArgs: BootIndex, TimeOffset(100us), PostCode(hex)
3511         std::ostringstream hexCode;
3512         hexCode << "0x" << std::setfill('0') << std::setw(2) << std::hex
3513                 << std::get<0>(code.second);
3514         std::ostringstream timeOffsetStr;
3515         // Set Fixed -Point Notation
3516         timeOffsetStr << std::fixed;
3517         // Set precision to 4 digits
3518         timeOffsetStr << std::setprecision(4);
3519         // Add double to stream
3520         timeOffsetStr << static_cast<double>(usTimeOffset) / 1000 / 1000;
3521         std::vector<std::string> messageArgs = {
3522             std::to_string(bootIndex), timeOffsetStr.str(), hexCode.str()};
3523 
3524         // Get MessageArgs template from message registry
3525         std::string msg;
3526         if (message != nullptr)
3527         {
3528             msg = message->message;
3529 
3530             // fill in this post code value
3531             int i = 0;
3532             for (const std::string& messageArg : messageArgs)
3533             {
3534                 std::string argStr = "%" + std::to_string(++i);
3535                 size_t argPos = msg.find(argStr);
3536                 if (argPos != std::string::npos)
3537                 {
3538                     msg.replace(argPos, argStr.length(), messageArg);
3539                 }
3540             }
3541         }
3542 
3543         // Get Severity template from message registry
3544         std::string severity;
3545         if (message != nullptr)
3546         {
3547             severity = message->messageSeverity;
3548         }
3549 
3550         // add to AsyncResp
3551         logEntryArray.push_back({});
3552         nlohmann::json& bmcLogEntry = logEntryArray.back();
3553         bmcLogEntry["@odata.type"] = "#LogEntry.v1_9_0.LogEntry";
3554         bmcLogEntry["@odata.id"] =
3555             "/redfish/v1/Systems/system/LogServices/PostCodes/Entries/" +
3556             postcodeEntryID;
3557         bmcLogEntry["Name"] = "POST Code Log Entry";
3558         bmcLogEntry["Id"] = postcodeEntryID;
3559         bmcLogEntry["Message"] = std::move(msg);
3560         bmcLogEntry["MessageId"] = "OpenBMC.0.2.BIOSPOSTCode";
3561         bmcLogEntry["MessageArgs"] = std::move(messageArgs);
3562         bmcLogEntry["EntryType"] = "Event";
3563         bmcLogEntry["Severity"] = std::move(severity);
3564         bmcLogEntry["Created"] = entryTimeStr;
3565         if (!std::get<std::vector<uint8_t>>(code.second).empty())
3566         {
3567             bmcLogEntry["AdditionalDataURI"] =
3568                 "/redfish/v1/Systems/system/LogServices/PostCodes/Entries/" +
3569                 postcodeEntryID + "/attachment";
3570         }
3571     }
3572 }
3573 
3574 static void getPostCodeForEntry(const std::shared_ptr<bmcweb::AsyncResp>& aResp,
3575                                 const uint16_t bootIndex,
3576                                 const uint64_t codeIndex)
3577 {
3578     crow::connections::systemBus->async_method_call(
3579         [aResp, bootIndex,
3580          codeIndex](const boost::system::error_code ec,
3581                     const boost::container::flat_map<
3582                         uint64_t, std::tuple<uint64_t, std::vector<uint8_t>>>&
3583                         postcode) {
3584         if (ec)
3585         {
3586             BMCWEB_LOG_DEBUG << "DBUS POST CODE PostCode response error";
3587             messages::internalError(aResp->res);
3588             return;
3589         }
3590 
3591         // skip the empty postcode boots
3592         if (postcode.empty())
3593         {
3594             return;
3595         }
3596 
3597         fillPostCodeEntry(aResp, postcode, bootIndex, codeIndex);
3598 
3599         aResp->res.jsonValue["Members@odata.count"] =
3600             aResp->res.jsonValue["Members"].size();
3601         },
3602         "xyz.openbmc_project.State.Boot.PostCode0",
3603         "/xyz/openbmc_project/State/Boot/PostCode0",
3604         "xyz.openbmc_project.State.Boot.PostCode", "GetPostCodesWithTimeStamp",
3605         bootIndex);
3606 }
3607 
3608 static void getPostCodeForBoot(const std::shared_ptr<bmcweb::AsyncResp>& aResp,
3609                                const uint16_t bootIndex,
3610                                const uint16_t bootCount,
3611                                const uint64_t entryCount, size_t skip,
3612                                size_t top)
3613 {
3614     crow::connections::systemBus->async_method_call(
3615         [aResp, bootIndex, bootCount, entryCount, skip,
3616          top](const boost::system::error_code ec,
3617               const boost::container::flat_map<
3618                   uint64_t, std::tuple<uint64_t, std::vector<uint8_t>>>&
3619                   postcode) {
3620         if (ec)
3621         {
3622             BMCWEB_LOG_DEBUG << "DBUS POST CODE PostCode response error";
3623             messages::internalError(aResp->res);
3624             return;
3625         }
3626 
3627         uint64_t endCount = entryCount;
3628         if (!postcode.empty())
3629         {
3630             endCount = entryCount + postcode.size();
3631             if (skip < endCount && (top + skip) > entryCount)
3632             {
3633                 uint64_t thisBootSkip =
3634                     std::max(static_cast<uint64_t>(skip), entryCount) -
3635                     entryCount;
3636                 uint64_t thisBootTop =
3637                     std::min(static_cast<uint64_t>(top + skip), endCount) -
3638                     entryCount;
3639 
3640                 fillPostCodeEntry(aResp, postcode, bootIndex, 0, thisBootSkip,
3641                                   thisBootTop);
3642             }
3643             aResp->res.jsonValue["Members@odata.count"] = endCount;
3644         }
3645 
3646         // continue to previous bootIndex
3647         if (bootIndex < bootCount)
3648         {
3649             getPostCodeForBoot(aResp, static_cast<uint16_t>(bootIndex + 1),
3650                                bootCount, endCount, skip, top);
3651         }
3652         else if (skip + top < endCount)
3653         {
3654             aResp->res.jsonValue["Members@odata.nextLink"] =
3655                 "/redfish/v1/Systems/system/LogServices/PostCodes/Entries?$skip=" +
3656                 std::to_string(skip + top);
3657         }
3658         },
3659         "xyz.openbmc_project.State.Boot.PostCode0",
3660         "/xyz/openbmc_project/State/Boot/PostCode0",
3661         "xyz.openbmc_project.State.Boot.PostCode", "GetPostCodesWithTimeStamp",
3662         bootIndex);
3663 }
3664 
3665 static void
3666     getCurrentBootNumber(const std::shared_ptr<bmcweb::AsyncResp>& aResp,
3667                          size_t skip, size_t top)
3668 {
3669     uint64_t entryCount = 0;
3670     sdbusplus::asio::getProperty<uint16_t>(
3671         *crow::connections::systemBus,
3672         "xyz.openbmc_project.State.Boot.PostCode0",
3673         "/xyz/openbmc_project/State/Boot/PostCode0",
3674         "xyz.openbmc_project.State.Boot.PostCode", "CurrentBootCycleCount",
3675         [aResp, entryCount, skip, top](const boost::system::error_code ec,
3676                                        const uint16_t bootCount) {
3677         if (ec)
3678         {
3679             BMCWEB_LOG_DEBUG << "DBUS response error " << ec;
3680             messages::internalError(aResp->res);
3681             return;
3682         }
3683         getPostCodeForBoot(aResp, 1, bootCount, entryCount, skip, top);
3684         });
3685 }
3686 
3687 inline void requestRoutesPostCodesEntryCollection(App& app)
3688 {
3689     BMCWEB_ROUTE(app,
3690                  "/redfish/v1/Systems/<str>/LogServices/PostCodes/Entries/")
3691         .privileges(redfish::privileges::getLogEntryCollection)
3692         .methods(boost::beast::http::verb::get)(
3693             [&app](const crow::Request& req,
3694                    const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
3695                    const std::string& systemName) {
3696         query_param::QueryCapabilities capabilities = {
3697             .canDelegateTop = true,
3698             .canDelegateSkip = true,
3699         };
3700         query_param::Query delegatedQuery;
3701         if (!redfish::setUpRedfishRouteWithDelegation(
3702                 app, req, asyncResp, delegatedQuery, capabilities))
3703         {
3704             return;
3705         }
3706 
3707         if (systemName != "system")
3708         {
3709             messages::resourceNotFound(asyncResp->res, "ComputerSystem",
3710                                        systemName);
3711             ;
3712             return;
3713         }
3714         asyncResp->res.jsonValue["@odata.type"] =
3715             "#LogEntryCollection.LogEntryCollection";
3716         asyncResp->res.jsonValue["@odata.id"] =
3717             "/redfish/v1/Systems/system/LogServices/PostCodes/Entries";
3718         asyncResp->res.jsonValue["Name"] = "BIOS POST Code Log Entries";
3719         asyncResp->res.jsonValue["Description"] =
3720             "Collection of POST Code Log Entries";
3721         asyncResp->res.jsonValue["Members"] = nlohmann::json::array();
3722         asyncResp->res.jsonValue["Members@odata.count"] = 0;
3723         size_t skip = delegatedQuery.skip.value_or(0);
3724         size_t top = delegatedQuery.top.value_or(query_param::Query::maxTop);
3725         getCurrentBootNumber(asyncResp, skip, top);
3726         });
3727 }
3728 
3729 /**
3730  * @brief Parse post code ID and get the current value and index value
3731  *        eg: postCodeID=B1-2, currentValue=1, index=2
3732  *
3733  * @param[in]  postCodeID     Post Code ID
3734  * @param[out] currentValue   Current value
3735  * @param[out] index          Index value
3736  *
3737  * @return bool true if the parsing is successful, false the parsing fails
3738  */
3739 inline static bool parsePostCode(const std::string& postCodeID,
3740                                  uint64_t& currentValue, uint16_t& index)
3741 {
3742     std::vector<std::string> split;
3743     boost::algorithm::split(split, postCodeID, boost::is_any_of("-"));
3744     if (split.size() != 2 || split[0].length() < 2 || split[0].front() != 'B')
3745     {
3746         return false;
3747     }
3748 
3749     // NOLINTNEXTLINE(cppcoreguidelines-pro-bounds-pointer-arithmetic)
3750     const char* start = split[0].data() + 1;
3751     // NOLINTNEXTLINE(cppcoreguidelines-pro-bounds-pointer-arithmetic)
3752     const char* end = split[0].data() + split[0].size();
3753     auto [ptrIndex, ecIndex] = std::from_chars(start, end, index);
3754 
3755     if (ptrIndex != end || ecIndex != std::errc())
3756     {
3757         return false;
3758     }
3759 
3760     start = split[1].data();
3761 
3762     // NOLINTNEXTLINE(cppcoreguidelines-pro-bounds-pointer-arithmetic)
3763     end = split[1].data() + split[1].size();
3764     auto [ptrValue, ecValue] = std::from_chars(start, end, currentValue);
3765 
3766     return ptrValue == end && ecValue == std::errc();
3767 }
3768 
3769 inline void requestRoutesPostCodesEntryAdditionalData(App& app)
3770 {
3771     BMCWEB_ROUTE(
3772         app,
3773         "/redfish/v1/Systems/<str>/LogServices/PostCodes/Entries/<str>/attachment/")
3774         .privileges(redfish::privileges::getLogEntry)
3775         .methods(boost::beast::http::verb::get)(
3776             [&app](const crow::Request& req,
3777                    const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
3778                    const std::string& systemName,
3779                    const std::string& postCodeID) {
3780         if (!redfish::setUpRedfishRoute(app, req, asyncResp))
3781         {
3782             return;
3783         }
3784         if (http_helpers::isContentTypeAllowed(
3785                 req.getHeaderValue("Accept"),
3786                 http_helpers::ContentType::OctetStream, true))
3787         {
3788             asyncResp->res.result(boost::beast::http::status::bad_request);
3789             return;
3790         }
3791         if (systemName != "system")
3792         {
3793             messages::resourceNotFound(asyncResp->res, "ComputerSystem",
3794                                        systemName);
3795             ;
3796             return;
3797         }
3798 
3799         uint64_t currentValue = 0;
3800         uint16_t index = 0;
3801         if (!parsePostCode(postCodeID, currentValue, index))
3802         {
3803             messages::resourceNotFound(asyncResp->res, "LogEntry", postCodeID);
3804             return;
3805         }
3806 
3807         crow::connections::systemBus->async_method_call(
3808             [asyncResp, postCodeID, currentValue](
3809                 const boost::system::error_code ec,
3810                 const std::vector<std::tuple<uint64_t, std::vector<uint8_t>>>&
3811                     postcodes) {
3812             if (ec.value() == EBADR)
3813             {
3814                 messages::resourceNotFound(asyncResp->res, "LogEntry",
3815                                            postCodeID);
3816                 return;
3817             }
3818             if (ec)
3819             {
3820                 BMCWEB_LOG_DEBUG << "DBUS response error " << ec;
3821                 messages::internalError(asyncResp->res);
3822                 return;
3823             }
3824 
3825             size_t value = static_cast<size_t>(currentValue) - 1;
3826             if (value == std::string::npos || postcodes.size() < currentValue)
3827             {
3828                 BMCWEB_LOG_ERROR << "Wrong currentValue value";
3829                 messages::resourceNotFound(asyncResp->res, "LogEntry",
3830                                            postCodeID);
3831                 return;
3832             }
3833 
3834             const auto& [tID, c] = postcodes[value];
3835             if (c.empty())
3836             {
3837                 BMCWEB_LOG_INFO << "No found post code data";
3838                 messages::resourceNotFound(asyncResp->res, "LogEntry",
3839                                            postCodeID);
3840                 return;
3841             }
3842             // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
3843             const char* d = reinterpret_cast<const char*>(c.data());
3844             std::string_view strData(d, c.size());
3845 
3846             asyncResp->res.addHeader(boost::beast::http::field::content_type,
3847                                      "application/octet-stream");
3848             asyncResp->res.addHeader(
3849                 boost::beast::http::field::content_transfer_encoding, "Base64");
3850             asyncResp->res.body() = crow::utility::base64encode(strData);
3851             },
3852             "xyz.openbmc_project.State.Boot.PostCode0",
3853             "/xyz/openbmc_project/State/Boot/PostCode0",
3854             "xyz.openbmc_project.State.Boot.PostCode", "GetPostCodes", index);
3855         });
3856 }
3857 
3858 inline void requestRoutesPostCodesEntry(App& app)
3859 {
3860     BMCWEB_ROUTE(
3861         app, "/redfish/v1/Systems/<str>/LogServices/PostCodes/Entries/<str>/")
3862         .privileges(redfish::privileges::getLogEntry)
3863         .methods(boost::beast::http::verb::get)(
3864             [&app](const crow::Request& req,
3865                    const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
3866                    const std::string& systemName, const std::string& targetID) {
3867         if (!redfish::setUpRedfishRoute(app, req, asyncResp))
3868         {
3869             return;
3870         }
3871         if (systemName != "system")
3872         {
3873             messages::resourceNotFound(asyncResp->res, "ComputerSystem",
3874                                        systemName);
3875             return;
3876         }
3877 
3878         uint16_t bootIndex = 0;
3879         uint64_t codeIndex = 0;
3880         if (!parsePostCode(targetID, codeIndex, bootIndex))
3881         {
3882             // Requested ID was not found
3883             messages::resourceMissingAtURI(asyncResp->res, req.urlView);
3884             return;
3885         }
3886         if (bootIndex == 0 || codeIndex == 0)
3887         {
3888             BMCWEB_LOG_DEBUG << "Get Post Code invalid entry string "
3889                              << targetID;
3890         }
3891 
3892         asyncResp->res.jsonValue["@odata.type"] = "#LogEntry.v1_9_0.LogEntry";
3893         asyncResp->res.jsonValue["@odata.id"] =
3894             "/redfish/v1/Systems/system/LogServices/PostCodes/Entries";
3895         asyncResp->res.jsonValue["Name"] = "BIOS POST Code Log Entries";
3896         asyncResp->res.jsonValue["Description"] =
3897             "Collection of POST Code Log Entries";
3898         asyncResp->res.jsonValue["Members"] = nlohmann::json::array();
3899         asyncResp->res.jsonValue["Members@odata.count"] = 0;
3900 
3901         getPostCodeForEntry(asyncResp, bootIndex, codeIndex);
3902         });
3903 }
3904 
3905 } // namespace redfish
3906