blob: f80776e3548919fcf270b0b90115a587a219ac75 [file] [log] [blame]
Lei YU5e0dcb32019-08-02 18:04:34 +08001#include "config.h"
2
3#include "utils.hpp"
4
Lei YUad90ad52019-08-06 11:19:28 +08005#include <openssl/sha.h>
6
Lei YU5e0dcb32019-08-02 18:04:34 +08007#include <fstream>
8
9namespace utils
10{
11
12namespace // anonymous
13{
14constexpr auto MAPPER_BUSNAME = "xyz.openbmc_project.ObjectMapper";
15constexpr auto MAPPER_PATH = "/xyz/openbmc_project/object_mapper";
16constexpr auto MAPPER_INTERFACE = "xyz.openbmc_project.ObjectMapper";
17} // namespace
18
19std::vector<std::string> getPSUInventoryPath(sdbusplus::bus::bus& bus)
20{
21 std::vector<std::string> paths;
22 auto method = bus.new_method_call(MAPPER_BUSNAME, MAPPER_PATH,
23 MAPPER_INTERFACE, "GetSubTreePaths");
24 method.append(PSU_INVENTORY_PATH_BASE);
25 method.append(0); // Depth 0 to search all
26 method.append(std::vector<std::string>({PSU_INVENTORY_IFACE}));
27 auto reply = bus.call(method);
28
29 reply.read(paths);
30 return paths;
31}
32
Lei YUad90ad52019-08-06 11:19:28 +080033std::string getService(sdbusplus::bus::bus& bus, const char* path,
34 const char* interface)
35{
36 auto mapper = bus.new_method_call(MAPPER_BUSNAME, MAPPER_PATH,
37 MAPPER_INTERFACE, "GetObject");
38
39 mapper.append(path, std::vector<std::string>({interface}));
40 try
41 {
42 auto mapperResponseMsg = bus.call(mapper);
43
44 std::vector<std::pair<std::string, std::vector<std::string>>>
45 mapperResponse;
46 mapperResponseMsg.read(mapperResponse);
47 if (mapperResponse.empty())
48 {
49 log<level::ERR>("Error reading mapper response");
50 throw std::runtime_error("Error reading mapper response");
51 }
52 if (mapperResponse.size() < 1)
53 {
54 return "";
55 }
56 return mapperResponse[0].first;
57 }
58 catch (const sdbusplus::exception::SdBusError& ex)
59 {
60 log<level::ERR>("Mapper call failed", entry("METHOD=%d", "GetObject"),
61 entry("PATH=%s", path),
62 entry("INTERFACE=%s", interface));
63 throw std::runtime_error("Mapper call failed");
64 }
65}
66
67std::string getVersionId(const std::string& version)
68{
69 if (version.empty())
70 {
71 log<level::ERR>("Error version is empty");
72 return {};
73 }
74
75 unsigned char digest[SHA512_DIGEST_LENGTH];
76 SHA512_CTX ctx;
77 SHA512_Init(&ctx);
78 SHA512_Update(&ctx, version.c_str(), strlen(version.c_str()));
79 SHA512_Final(digest, &ctx);
80 char mdString[SHA512_DIGEST_LENGTH * 2 + 1];
81 for (int i = 0; i < SHA512_DIGEST_LENGTH; i++)
82 {
83 snprintf(&mdString[i * 2], 3, "%02x", (unsigned int)digest[i]);
84 }
85
86 // Only need 8 hex digits.
87 std::string hexId = std::string(mdString);
88 return (hexId.substr(0, 8));
89}
90
Lei YU5e0dcb32019-08-02 18:04:34 +080091} // namespace utils