NodeMan97 | 27d1e14 | 2022-07-27 15:10:07 -0500 | [diff] [blame] | 1 | #include "config.h" |
| 2 | |
Carol Wang | dc05939 | 2020-03-13 17:39:17 +0800 | [diff] [blame] | 3 | #include "utils.hpp" |
| 4 | |
NodeMan97 | 27d1e14 | 2022-07-27 15:10:07 -0500 | [diff] [blame] | 5 | #include <fmt/format.h> |
| 6 | #include <fmt/printf.h> |
Andrew Geissler | f8ae6a0 | 2022-01-21 17:00:20 -0600 | [diff] [blame] | 7 | #include <gpiod.h> |
| 8 | |
Andrew Geissler | 8ffdb26 | 2021-09-20 15:25:19 -0500 | [diff] [blame] | 9 | #include <phosphor-logging/lg2.hpp> |
Carol Wang | dc05939 | 2020-03-13 17:39:17 +0800 | [diff] [blame] | 10 | |
NodeMan97 | 27d1e14 | 2022-07-27 15:10:07 -0500 | [diff] [blame] | 11 | #include <filesystem> |
| 12 | |
Carol Wang | dc05939 | 2020-03-13 17:39:17 +0800 | [diff] [blame] | 13 | namespace phosphor |
| 14 | { |
| 15 | namespace state |
| 16 | { |
| 17 | namespace manager |
| 18 | { |
| 19 | namespace utils |
| 20 | { |
| 21 | |
Andrew Geissler | 8ffdb26 | 2021-09-20 15:25:19 -0500 | [diff] [blame] | 22 | PHOSPHOR_LOG2_USING; |
Carol Wang | dc05939 | 2020-03-13 17:39:17 +0800 | [diff] [blame] | 23 | |
Andrew Geissler | 928bbf1 | 2023-02-14 13:30:14 -0600 | [diff] [blame^] | 24 | constexpr auto SYSTEMD_SERVICE = "org.freedesktop.systemd1"; |
| 25 | constexpr auto SYSTEMD_OBJ_PATH = "/org/freedesktop/systemd1"; |
| 26 | constexpr auto SYSTEMD_INTERFACE = "org.freedesktop.systemd1.Manager"; |
| 27 | |
Carol Wang | dc05939 | 2020-03-13 17:39:17 +0800 | [diff] [blame] | 28 | constexpr auto MAPPER_BUSNAME = "xyz.openbmc_project.ObjectMapper"; |
| 29 | constexpr auto MAPPER_PATH = "/xyz/openbmc_project/object_mapper"; |
| 30 | constexpr auto MAPPER_INTERFACE = "xyz.openbmc_project.ObjectMapper"; |
| 31 | constexpr auto PROPERTY_INTERFACE = "org.freedesktop.DBus.Properties"; |
| 32 | |
Andrew Geissler | 928bbf1 | 2023-02-14 13:30:14 -0600 | [diff] [blame^] | 33 | void subscribeToSystemdSignals(sdbusplus::bus::bus& bus) |
| 34 | { |
| 35 | auto method = bus.new_method_call(SYSTEMD_SERVICE, SYSTEMD_OBJ_PATH, |
| 36 | SYSTEMD_INTERFACE, "Subscribe"); |
| 37 | |
| 38 | try |
| 39 | { |
| 40 | bus.call(method); |
| 41 | } |
| 42 | catch (const sdbusplus::exception_t& e) |
| 43 | { |
| 44 | error("Failed to subscribe to systemd signals: {ERROR}", "ERROR", e); |
| 45 | throw std::runtime_error("Unable to subscribe to systemd signals"); |
| 46 | } |
| 47 | return; |
| 48 | } |
| 49 | |
| 50 | std::string getService(sdbusplus::bus::bus& bus, std::string path, |
Carol Wang | dc05939 | 2020-03-13 17:39:17 +0800 | [diff] [blame] | 51 | std::string interface) |
| 52 | { |
| 53 | auto mapper = bus.new_method_call(MAPPER_BUSNAME, MAPPER_PATH, |
| 54 | MAPPER_INTERFACE, "GetObject"); |
| 55 | |
| 56 | mapper.append(path, std::vector<std::string>({interface})); |
| 57 | |
| 58 | std::vector<std::pair<std::string, std::vector<std::string>>> |
| 59 | mapperResponse; |
| 60 | |
| 61 | try |
| 62 | { |
| 63 | auto mapperResponseMsg = bus.call(mapper); |
| 64 | |
| 65 | mapperResponseMsg.read(mapperResponse); |
| 66 | if (mapperResponse.empty()) |
| 67 | { |
Andrew Geissler | ad65b2d | 2021-09-21 12:53:29 -0500 | [diff] [blame] | 68 | error( |
| 69 | "Error no matching service with path {PATH} and interface {INTERFACE}", |
| 70 | "PATH", path, "INTERFACE", interface); |
Carol Wang | dc05939 | 2020-03-13 17:39:17 +0800 | [diff] [blame] | 71 | throw std::runtime_error("Error no matching service"); |
| 72 | } |
| 73 | } |
Patrick Williams | f053e6f | 2022-07-22 19:26:54 -0500 | [diff] [blame] | 74 | catch (const sdbusplus::exception_t& e) |
Carol Wang | dc05939 | 2020-03-13 17:39:17 +0800 | [diff] [blame] | 75 | { |
Andrew Geissler | 8ffdb26 | 2021-09-20 15:25:19 -0500 | [diff] [blame] | 76 | error("Error in mapper call with path {PATH}, interface " |
| 77 | "{INTERFACE}, and exception {ERROR}", |
| 78 | "PATH", path, "INTERFACE", interface, "ERROR", e); |
Carol Wang | dc05939 | 2020-03-13 17:39:17 +0800 | [diff] [blame] | 79 | throw; |
| 80 | } |
| 81 | |
| 82 | return mapperResponse.begin()->first; |
| 83 | } |
| 84 | |
Patrick Williams | f053e6f | 2022-07-22 19:26:54 -0500 | [diff] [blame] | 85 | std::string getProperty(sdbusplus::bus_t& bus, const std::string& path, |
Andrew Geissler | 49e6713 | 2022-01-26 14:27:52 -0600 | [diff] [blame] | 86 | const std::string& interface, |
| 87 | const std::string& propertyName) |
| 88 | { |
| 89 | std::variant<std::string> property; |
| 90 | std::string service = getService(bus, path, interface); |
| 91 | |
| 92 | auto method = bus.new_method_call(service.c_str(), path.c_str(), |
| 93 | PROPERTY_INTERFACE, "Get"); |
| 94 | |
| 95 | method.append(interface, propertyName); |
| 96 | |
| 97 | try |
| 98 | { |
| 99 | auto reply = bus.call(method); |
| 100 | reply.read(property); |
| 101 | } |
Patrick Williams | f053e6f | 2022-07-22 19:26:54 -0500 | [diff] [blame] | 102 | catch (const sdbusplus::exception_t& e) |
Andrew Geissler | 49e6713 | 2022-01-26 14:27:52 -0600 | [diff] [blame] | 103 | { |
| 104 | error("Error in property Get, error {ERROR}, property {PROPERTY}", |
| 105 | "ERROR", e, "PROPERTY", propertyName); |
| 106 | throw; |
| 107 | } |
| 108 | |
| 109 | if (std::get<std::string>(property).empty()) |
| 110 | { |
| 111 | error("Error reading property response for {PROPERTY}", "PROPERTY", |
| 112 | propertyName); |
| 113 | throw std::runtime_error("Error reading property response"); |
| 114 | } |
| 115 | |
| 116 | return std::get<std::string>(property); |
| 117 | } |
| 118 | |
Patrick Williams | f053e6f | 2022-07-22 19:26:54 -0500 | [diff] [blame] | 119 | void setProperty(sdbusplus::bus_t& bus, const std::string& path, |
Carol Wang | dc05939 | 2020-03-13 17:39:17 +0800 | [diff] [blame] | 120 | const std::string& interface, const std::string& property, |
| 121 | const std::string& value) |
| 122 | { |
Patrick Williams | 2975e26 | 2020-05-13 18:01:09 -0500 | [diff] [blame] | 123 | std::variant<std::string> variantValue = value; |
Carol Wang | dc05939 | 2020-03-13 17:39:17 +0800 | [diff] [blame] | 124 | std::string service = getService(bus, path, interface); |
| 125 | |
| 126 | auto method = bus.new_method_call(service.c_str(), path.c_str(), |
| 127 | PROPERTY_INTERFACE, "Set"); |
| 128 | |
| 129 | method.append(interface, property, variantValue); |
| 130 | bus.call_noreply(method); |
| 131 | |
| 132 | return; |
| 133 | } |
| 134 | |
Andrew Geissler | f8ae6a0 | 2022-01-21 17:00:20 -0600 | [diff] [blame] | 135 | int getGpioValue(const std::string& gpioName) |
| 136 | { |
Andrew Geissler | f8ae6a0 | 2022-01-21 17:00:20 -0600 | [diff] [blame] | 137 | int gpioval = -1; |
| 138 | gpiod_line* line = gpiod_line_find(gpioName.c_str()); |
| 139 | |
| 140 | if (nullptr != line) |
| 141 | { |
| 142 | // take ownership of gpio |
| 143 | if (0 != gpiod_line_request_input(line, "state-manager")) |
| 144 | { |
| 145 | error("Failed request for {GPIO_NAME} GPIO", "GPIO_NAME", gpioName); |
| 146 | } |
| 147 | else |
| 148 | { |
| 149 | // get gpio value |
| 150 | gpioval = gpiod_line_get_value(line); |
| 151 | |
| 152 | // release ownership of gpio |
| 153 | gpiod_line_close_chip(line); |
| 154 | } |
| 155 | } |
| 156 | return gpioval; |
| 157 | } |
| 158 | |
Andrew Geissler | 9d4d0c9 | 2022-01-26 13:18:12 -0600 | [diff] [blame] | 159 | void createError( |
Patrick Williams | f053e6f | 2022-07-22 19:26:54 -0500 | [diff] [blame] | 160 | sdbusplus::bus_t& bus, const std::string& errorMsg, |
Andrew Geissler | d49f51e | 2022-03-07 14:57:07 -0600 | [diff] [blame] | 161 | sdbusplus::xyz::openbmc_project::Logging::server::Entry::Level errLevel, |
| 162 | std::map<std::string, std::string> additionalData) |
Andrew Geissler | 9d4d0c9 | 2022-01-26 13:18:12 -0600 | [diff] [blame] | 163 | { |
Andrew Geissler | 9d4d0c9 | 2022-01-26 13:18:12 -0600 | [diff] [blame] | 164 | try |
| 165 | { |
Andrew Geissler | d49f51e | 2022-03-07 14:57:07 -0600 | [diff] [blame] | 166 | // Always add the _PID on for some extra logging debug |
Andrew Geissler | 9d4d0c9 | 2022-01-26 13:18:12 -0600 | [diff] [blame] | 167 | additionalData.emplace("_PID", std::to_string(getpid())); |
| 168 | |
| 169 | auto method = bus.new_method_call( |
| 170 | "xyz.openbmc_project.Logging", "/xyz/openbmc_project/logging", |
| 171 | "xyz.openbmc_project.Logging.Create", "Create"); |
| 172 | |
| 173 | method.append(errorMsg, errLevel, additionalData); |
| 174 | auto resp = bus.call(method); |
| 175 | } |
Patrick Williams | f053e6f | 2022-07-22 19:26:54 -0500 | [diff] [blame] | 176 | catch (const sdbusplus::exception_t& e) |
Andrew Geissler | 9d4d0c9 | 2022-01-26 13:18:12 -0600 | [diff] [blame] | 177 | { |
| 178 | error("sdbusplus D-Bus call exception, error {ERROR} trying to create " |
| 179 | "an error with {ERROR_MSG}", |
| 180 | "ERROR", e, "ERROR_MSG", errorMsg); |
| 181 | |
| 182 | throw std::runtime_error( |
| 183 | "Error in invoking D-Bus logging create interface"); |
| 184 | } |
| 185 | catch (const std::exception& e) |
| 186 | { |
| 187 | error("D-bus call exception: {ERROR}", "ERROR", e); |
| 188 | throw e; |
| 189 | } |
| 190 | } |
| 191 | |
Patrick Williams | f053e6f | 2022-07-22 19:26:54 -0500 | [diff] [blame] | 192 | void createBmcDump(sdbusplus::bus_t& bus) |
Andrew Geissler | 55e96ac | 2022-04-19 11:44:53 -0400 | [diff] [blame] | 193 | { |
| 194 | auto method = bus.new_method_call( |
| 195 | "xyz.openbmc_project.Dump.Manager", "/xyz/openbmc_project/dump/bmc", |
| 196 | "xyz.openbmc_project.Dump.Create", "CreateDump"); |
| 197 | method.append( |
| 198 | std::vector< |
| 199 | std::pair<std::string, std::variant<std::string, uint64_t>>>()); |
| 200 | try |
| 201 | { |
| 202 | bus.call_noreply(method); |
| 203 | } |
Patrick Williams | f053e6f | 2022-07-22 19:26:54 -0500 | [diff] [blame] | 204 | catch (const sdbusplus::exception_t& e) |
Andrew Geissler | 55e96ac | 2022-04-19 11:44:53 -0400 | [diff] [blame] | 205 | { |
| 206 | error("Failed to create BMC dump, exception:{ERROR}", "ERROR", e); |
| 207 | // just continue, this is error path anyway so we're just collecting |
| 208 | // what we can |
| 209 | } |
| 210 | } |
| 211 | |
NodeMan97 | 27d1e14 | 2022-07-27 15:10:07 -0500 | [diff] [blame] | 212 | bool checkACLoss(size_t& chassisId) |
| 213 | { |
| 214 | std::string chassisLostPowerFileFmt = |
| 215 | fmt::sprintf(CHASSIS_LOST_POWER_FILE, chassisId); |
| 216 | |
| 217 | std::filesystem::path chassisPowerLossFile{chassisLostPowerFileFmt}; |
| 218 | if (std::filesystem::exists(chassisPowerLossFile)) |
| 219 | { |
| 220 | return true; |
| 221 | } |
| 222 | |
| 223 | return false; |
| 224 | } |
| 225 | |
Carol Wang | dc05939 | 2020-03-13 17:39:17 +0800 | [diff] [blame] | 226 | } // namespace utils |
| 227 | } // namespace manager |
| 228 | } // namespace state |
| 229 | } // namespace phosphor |