Lei YU | 5e0dcb3 | 2019-08-02 18:04:34 +0800 | [diff] [blame] | 1 | #include "config.h" |
| 2 | |
| 3 | #include "utils.hpp" |
| 4 | |
Lei YU | ad90ad5 | 2019-08-06 11:19:28 +0800 | [diff] [blame] | 5 | #include <openssl/sha.h> |
| 6 | |
Lei YU | 5e0dcb3 | 2019-08-02 18:04:34 +0800 | [diff] [blame] | 7 | #include <fstream> |
Lei YU | f77189f | 2019-08-07 14:26:30 +0800 | [diff] [blame] | 8 | #include <phosphor-logging/log.hpp> |
Lei YU | 5f3584d | 2019-08-27 16:28:53 +0800 | [diff] [blame] | 9 | #include <sstream> |
Lei YU | f77189f | 2019-08-07 14:26:30 +0800 | [diff] [blame] | 10 | |
| 11 | using namespace phosphor::logging; |
Lei YU | 5e0dcb3 | 2019-08-02 18:04:34 +0800 | [diff] [blame] | 12 | |
| 13 | namespace utils |
| 14 | { |
| 15 | |
| 16 | namespace // anonymous |
| 17 | { |
| 18 | constexpr auto MAPPER_BUSNAME = "xyz.openbmc_project.ObjectMapper"; |
| 19 | constexpr auto MAPPER_PATH = "/xyz/openbmc_project/object_mapper"; |
| 20 | constexpr auto MAPPER_INTERFACE = "xyz.openbmc_project.ObjectMapper"; |
| 21 | } // namespace |
| 22 | |
Lei YU | 5f3584d | 2019-08-27 16:28:53 +0800 | [diff] [blame] | 23 | namespace internal |
| 24 | { |
| 25 | template <typename... Ts> |
| 26 | std::string concat_string(Ts const&... ts) |
| 27 | { |
| 28 | std::stringstream s; |
| 29 | ((s << ts << " "), ...) << std::endl; |
| 30 | return s.str(); |
| 31 | } |
| 32 | |
| 33 | // Helper function to run command |
| 34 | // Returns return code and the stdout |
| 35 | template <typename... Ts> |
| 36 | std::pair<int, std::string> exec(Ts const&... ts) |
| 37 | { |
| 38 | std::array<char, 512> buffer; |
| 39 | std::string cmd = concat_string(ts...); |
| 40 | std::stringstream result; |
| 41 | int rc; |
| 42 | FILE* pipe = popen(cmd.c_str(), "r"); |
| 43 | if (!pipe) |
| 44 | { |
| 45 | throw std::runtime_error("popen() failed!"); |
| 46 | } |
| 47 | while (fgets(buffer.data(), buffer.size(), pipe) != nullptr) |
| 48 | { |
| 49 | result << buffer.data(); |
| 50 | } |
| 51 | rc = pclose(pipe); |
| 52 | return {rc, result.str()}; |
| 53 | } |
| 54 | |
| 55 | } // namespace internal |
Lei YU | f77189f | 2019-08-07 14:26:30 +0800 | [diff] [blame] | 56 | const UtilsInterface& getUtils() |
| 57 | { |
| 58 | static Utils utils; |
| 59 | return utils; |
| 60 | } |
| 61 | |
| 62 | std::vector<std::string> |
| 63 | Utils::getPSUInventoryPath(sdbusplus::bus::bus& bus) const |
Lei YU | 5e0dcb3 | 2019-08-02 18:04:34 +0800 | [diff] [blame] | 64 | { |
| 65 | std::vector<std::string> paths; |
| 66 | auto method = bus.new_method_call(MAPPER_BUSNAME, MAPPER_PATH, |
| 67 | MAPPER_INTERFACE, "GetSubTreePaths"); |
| 68 | method.append(PSU_INVENTORY_PATH_BASE); |
| 69 | method.append(0); // Depth 0 to search all |
| 70 | method.append(std::vector<std::string>({PSU_INVENTORY_IFACE})); |
| 71 | auto reply = bus.call(method); |
| 72 | |
| 73 | reply.read(paths); |
| 74 | return paths; |
| 75 | } |
| 76 | |
Lei YU | f77189f | 2019-08-07 14:26:30 +0800 | [diff] [blame] | 77 | std::string Utils::getService(sdbusplus::bus::bus& bus, const char* path, |
| 78 | const char* interface) const |
Lei YU | ad90ad5 | 2019-08-06 11:19:28 +0800 | [diff] [blame] | 79 | { |
Lei YU | d0bbfa9 | 2019-09-11 16:10:54 +0800 | [diff] [blame] | 80 | auto services = getServices(bus, path, interface); |
| 81 | if (services.empty()) |
| 82 | { |
| 83 | return {}; |
| 84 | } |
| 85 | return services[0]; |
| 86 | } |
| 87 | |
| 88 | std::vector<std::string> Utils::getServices(sdbusplus::bus::bus& bus, |
| 89 | const char* path, |
| 90 | const char* interface) const |
| 91 | { |
Lei YU | ad90ad5 | 2019-08-06 11:19:28 +0800 | [diff] [blame] | 92 | auto mapper = bus.new_method_call(MAPPER_BUSNAME, MAPPER_PATH, |
| 93 | MAPPER_INTERFACE, "GetObject"); |
| 94 | |
| 95 | mapper.append(path, std::vector<std::string>({interface})); |
| 96 | try |
| 97 | { |
| 98 | auto mapperResponseMsg = bus.call(mapper); |
| 99 | |
| 100 | std::vector<std::pair<std::string, std::vector<std::string>>> |
| 101 | mapperResponse; |
| 102 | mapperResponseMsg.read(mapperResponse); |
| 103 | if (mapperResponse.empty()) |
| 104 | { |
| 105 | log<level::ERR>("Error reading mapper response"); |
| 106 | throw std::runtime_error("Error reading mapper response"); |
| 107 | } |
Lei YU | d0bbfa9 | 2019-09-11 16:10:54 +0800 | [diff] [blame] | 108 | std::vector<std::string> ret; |
| 109 | for (const auto& i : mapperResponse) |
Lei YU | ad90ad5 | 2019-08-06 11:19:28 +0800 | [diff] [blame] | 110 | { |
Lei YU | d0bbfa9 | 2019-09-11 16:10:54 +0800 | [diff] [blame] | 111 | ret.emplace_back(i.first); |
Lei YU | ad90ad5 | 2019-08-06 11:19:28 +0800 | [diff] [blame] | 112 | } |
Lei YU | d0bbfa9 | 2019-09-11 16:10:54 +0800 | [diff] [blame] | 113 | return ret; |
Lei YU | ad90ad5 | 2019-08-06 11:19:28 +0800 | [diff] [blame] | 114 | } |
| 115 | catch (const sdbusplus::exception::SdBusError& ex) |
| 116 | { |
| 117 | log<level::ERR>("Mapper call failed", entry("METHOD=%d", "GetObject"), |
| 118 | entry("PATH=%s", path), |
| 119 | entry("INTERFACE=%s", interface)); |
| 120 | throw std::runtime_error("Mapper call failed"); |
| 121 | } |
| 122 | } |
| 123 | |
Lei YU | f77189f | 2019-08-07 14:26:30 +0800 | [diff] [blame] | 124 | std::string Utils::getVersionId(const std::string& version) const |
Lei YU | ad90ad5 | 2019-08-06 11:19:28 +0800 | [diff] [blame] | 125 | { |
| 126 | if (version.empty()) |
| 127 | { |
| 128 | log<level::ERR>("Error version is empty"); |
| 129 | return {}; |
| 130 | } |
| 131 | |
| 132 | unsigned char digest[SHA512_DIGEST_LENGTH]; |
| 133 | SHA512_CTX ctx; |
| 134 | SHA512_Init(&ctx); |
| 135 | SHA512_Update(&ctx, version.c_str(), strlen(version.c_str())); |
| 136 | SHA512_Final(digest, &ctx); |
| 137 | char mdString[SHA512_DIGEST_LENGTH * 2 + 1]; |
| 138 | for (int i = 0; i < SHA512_DIGEST_LENGTH; i++) |
| 139 | { |
| 140 | snprintf(&mdString[i * 2], 3, "%02x", (unsigned int)digest[i]); |
| 141 | } |
| 142 | |
| 143 | // Only need 8 hex digits. |
| 144 | std::string hexId = std::string(mdString); |
| 145 | return (hexId.substr(0, 8)); |
| 146 | } |
| 147 | |
Lei YU | 5f3584d | 2019-08-27 16:28:53 +0800 | [diff] [blame] | 148 | std::string Utils::getVersion(const std::string& inventoryPath) const |
| 149 | { |
| 150 | // Invoke vendor-specify tool to get the version string, e.g. |
| 151 | // psutils get-version |
| 152 | // /xyz/openbmc_project/inventory/system/chassis/motherboard/powersupply0 |
| 153 | auto [rc, r] = internal::exec(PSU_VERSION_UTIL, inventoryPath); |
| 154 | return (rc == 0) ? r : ""; |
| 155 | } |
| 156 | |
Lei YU | f77189f | 2019-08-07 14:26:30 +0800 | [diff] [blame] | 157 | any Utils::getPropertyImpl(sdbusplus::bus::bus& bus, const char* service, |
| 158 | const char* path, const char* interface, |
| 159 | const char* propertyName) const |
| 160 | { |
| 161 | auto method = bus.new_method_call(service, path, |
| 162 | "org.freedesktop.DBus.Properties", "Get"); |
| 163 | method.append(interface, propertyName); |
| 164 | try |
| 165 | { |
| 166 | PropertyType value{}; |
| 167 | auto reply = bus.call(method); |
| 168 | reply.read(value); |
| 169 | return any(value); |
| 170 | } |
| 171 | catch (const sdbusplus::exception::SdBusError& ex) |
| 172 | { |
| 173 | log<level::ERR>("GetProperty call failed", entry("PATH=%s", path), |
| 174 | entry("INTERFACE=%s", interface), |
| 175 | entry("PROPERTY=%s", propertyName)); |
| 176 | throw std::runtime_error("GetProperty call failed"); |
| 177 | } |
| 178 | } |
| 179 | |
Lei YU | 5e0dcb3 | 2019-08-02 18:04:34 +0800 | [diff] [blame] | 180 | } // namespace utils |