blob: 8290675642d235c819d7f71fdc5be36373e65975 [file] [log] [blame]
Sampa Misra032bd502019-03-06 05:03:22 -06001#include "utils.hpp"
2
3#include "xyz/openbmc_project/Common/error.hpp"
4
5#include <array>
6#include <ctime>
7#include <iostream>
8#include <map>
Sampa Misra032bd502019-03-06 05:03:22 -06009#include <stdexcept>
10#include <string>
11#include <vector>
12
13namespace pldm
14{
Sampa Misra032bd502019-03-06 05:03:22 -060015
16constexpr auto mapperBusName = "xyz.openbmc_project.ObjectMapper";
17constexpr auto mapperPath = "/xyz/openbmc_project/object_mapper";
18constexpr auto mapperInterface = "xyz.openbmc_project.ObjectMapper";
19
20namespace responder
21{
22
23std::string getService(sdbusplus::bus::bus& bus, const std::string& path,
24 const std::string& interface)
25{
26 using DbusInterfaceList = std::vector<std::string>;
27 std::map<std::string, std::vector<std::string>> mapperResponse;
28
29 try
30 {
31 auto mapper = bus.new_method_call(mapperBusName, mapperPath,
32 mapperInterface, "GetObject");
33 mapper.append(path, DbusInterfaceList({interface}));
34
35 auto mapperResponseMsg = bus.call(mapper);
36 mapperResponseMsg.read(mapperResponse);
37 }
38 catch (std::exception& e)
39 {
Sampa Misraaa8ae722019-12-12 03:20:40 -060040 std::cerr << "Error in mapper call, ERROR=" << e.what()
41 << " PATH=" << path.c_str()
42 << " INTERFACE=" << interface.c_str() << "\n";
Sampa Misra032bd502019-03-06 05:03:22 -060043 throw;
44 }
45 return mapperResponse.begin()->first;
46}
47
Sampa Misraaa8ae722019-12-12 03:20:40 -060048void reportError(const char* errorMsg)
49{
50 static constexpr auto logObjPath = "/xyz/openbmc_project/logging";
51 static constexpr auto logInterface = "xyz.openbmc_project.Logging.Create";
52
53 static sdbusplus::bus::bus bus = sdbusplus::bus::new_default();
54
55 try
56 {
57 auto service = getService(bus, logObjPath, logInterface);
58 using namespace sdbusplus::xyz::openbmc_project::Logging::server;
59 auto severity =
60 sdbusplus::xyz::openbmc_project::Logging::server::convertForMessage(
61 sdbusplus::xyz::openbmc_project::Logging::server::Entry::Level::
62 Error);
63 auto method = bus.new_method_call(service.c_str(), logObjPath,
64 logInterface, "Create");
65 std::map<std::string, std::string> addlData{};
66 method.append(errorMsg, severity, addlData);
67 bus.call_noreply(method);
68 }
69 catch (const std::exception& e)
70 {
71 std::cerr << "failed to make a d-bus call to create error log, ERROR="
72 << e.what() << "\n";
73 }
74}
75
Sampa Misrab37be312019-07-03 02:26:41 -050076namespace utils
77{
78
79uint8_t getNumPadBytes(uint32_t data)
80{
81 uint8_t pad;
82 pad = ((data % 4) ? (4 - data % 4) : 0);
83 return pad;
84} // end getNumPadBytes
85
86} // end namespace utils
Sampa Misra032bd502019-03-06 05:03:22 -060087} // namespace responder
88} // namespace pldm