1*93f06410SDhruvaraj Subhashchandran #include "config.h"
2*93f06410SDhruvaraj Subhashchandran
3d31be2ccSJayanth Othayoth #include "dump_utils.hpp"
4d31be2ccSJayanth Othayoth
53604710dSDhruvaraj Subhashchandran #include "dump_types.hpp"
63604710dSDhruvaraj Subhashchandran
7d1f670feSDhruvaraj Subhashchandran #include <phosphor-logging/lg2.hpp>
8d31be2ccSJayanth Othayoth
9*93f06410SDhruvaraj Subhashchandran #include <ctime>
10*93f06410SDhruvaraj Subhashchandran #include <filesystem>
11*93f06410SDhruvaraj Subhashchandran #include <optional>
12*93f06410SDhruvaraj Subhashchandran #include <regex>
13*93f06410SDhruvaraj Subhashchandran #include <tuple>
14*93f06410SDhruvaraj Subhashchandran
15d31be2ccSJayanth Othayoth namespace phosphor
16d31be2ccSJayanth Othayoth {
17d31be2ccSJayanth Othayoth namespace dump
18d31be2ccSJayanth Othayoth {
19d31be2ccSJayanth Othayoth
getService(sdbusplus::bus_t & bus,const std::string & path,const std::string & interface)209b18bf2dSPatrick Williams std::string getService(sdbusplus::bus_t& bus, const std::string& path,
21d31be2ccSJayanth Othayoth const std::string& interface)
22d31be2ccSJayanth Othayoth {
23d31be2ccSJayanth Othayoth constexpr auto objectMapperName = "xyz.openbmc_project.ObjectMapper";
24d31be2ccSJayanth Othayoth constexpr auto objectMapperPath = "/xyz/openbmc_project/object_mapper";
25d31be2ccSJayanth Othayoth
26d31be2ccSJayanth Othayoth auto method = bus.new_method_call(objectMapperName, objectMapperPath,
27d31be2ccSJayanth Othayoth objectMapperName, "GetObject");
28d31be2ccSJayanth Othayoth
29d31be2ccSJayanth Othayoth method.append(path);
30d31be2ccSJayanth Othayoth method.append(std::vector<std::string>({interface}));
31d31be2ccSJayanth Othayoth
32d31be2ccSJayanth Othayoth std::vector<std::pair<std::string, std::vector<std::string>>> response;
33d31be2ccSJayanth Othayoth
34d31be2ccSJayanth Othayoth try
35d31be2ccSJayanth Othayoth {
36d31be2ccSJayanth Othayoth auto reply = bus.call(method);
37d31be2ccSJayanth Othayoth reply.read(response);
38d31be2ccSJayanth Othayoth if (response.empty())
39d31be2ccSJayanth Othayoth {
40d1f670feSDhruvaraj Subhashchandran lg2::error(
41d1f670feSDhruvaraj Subhashchandran "Error in mapper response for getting service name, PATH: "
42d1f670feSDhruvaraj Subhashchandran "{PATH}, INTERFACE: {INTERFACE}",
43d1f670feSDhruvaraj Subhashchandran "PATH", path, "INTERFACE", interface);
44d31be2ccSJayanth Othayoth return std::string{};
45d31be2ccSJayanth Othayoth }
46d31be2ccSJayanth Othayoth }
479b18bf2dSPatrick Williams catch (const sdbusplus::exception_t& e)
48d31be2ccSJayanth Othayoth {
49d1f670feSDhruvaraj Subhashchandran lg2::error("Error in mapper method call, errormsg: {ERROR}, "
50d1f670feSDhruvaraj Subhashchandran "PATH: {PATH}, INTERFACE: {INTERFACE}",
51d1f670feSDhruvaraj Subhashchandran "ERROR", e, "PATH", path, "INTERFACE", interface);
523a25e5b2SDhruvaraj Subhashchandran throw;
53d31be2ccSJayanth Othayoth }
54d31be2ccSJayanth Othayoth return response[0].first;
55d31be2ccSJayanth Othayoth }
56d31be2ccSJayanth Othayoth
57*93f06410SDhruvaraj Subhashchandran std::optional<std::tuple<uint32_t, uint64_t, uint64_t>>
extractDumpDetails(const std::filesystem::path & file)58*93f06410SDhruvaraj Subhashchandran extractDumpDetails(const std::filesystem::path& file)
59*93f06410SDhruvaraj Subhashchandran {
60*93f06410SDhruvaraj Subhashchandran static constexpr auto ID_POS = 1;
61*93f06410SDhruvaraj Subhashchandran static constexpr auto EPOCHTIME_POS = 2;
62*93f06410SDhruvaraj Subhashchandran std::regex file_regex("obmcdump_([0-9]+)_([0-9]+).([a-zA-Z0-9]+)");
63*93f06410SDhruvaraj Subhashchandran
64*93f06410SDhruvaraj Subhashchandran std::smatch match;
65*93f06410SDhruvaraj Subhashchandran std::string name = file.filename().string();
66*93f06410SDhruvaraj Subhashchandran
67*93f06410SDhruvaraj Subhashchandran if (!((std::regex_search(name, match, file_regex)) && (match.size() > 0)))
68*93f06410SDhruvaraj Subhashchandran {
69*93f06410SDhruvaraj Subhashchandran lg2::error("Invalid Dump file name, FILENAME: {FILENAME}", "FILENAME",
70*93f06410SDhruvaraj Subhashchandran file);
71*93f06410SDhruvaraj Subhashchandran return std::nullopt;
72*93f06410SDhruvaraj Subhashchandran }
73*93f06410SDhruvaraj Subhashchandran
74*93f06410SDhruvaraj Subhashchandran auto idString = match[ID_POS];
75*93f06410SDhruvaraj Subhashchandran uint64_t timestamp = stoull(match[EPOCHTIME_POS]) * 1000 * 1000;
76*93f06410SDhruvaraj Subhashchandran
77*93f06410SDhruvaraj Subhashchandran return std::make_tuple(stoul(idString), timestamp,
78*93f06410SDhruvaraj Subhashchandran std::filesystem::file_size(file));
79*93f06410SDhruvaraj Subhashchandran }
80*93f06410SDhruvaraj Subhashchandran
81d31be2ccSJayanth Othayoth } // namespace dump
82d31be2ccSJayanth Othayoth } // namespace phosphor
83