Matt Spinler | 711d51d | 2019-11-06 09:36:51 -0600 | [diff] [blame] | 1 | /** |
| 2 | * Copyright © 2019 IBM Corporation |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
Matt Spinler | cad9c2b | 2019-12-02 15:42:01 -0600 | [diff] [blame] | 16 | #include "config.h" |
| 17 | |
Matt Spinler | c8705e2 | 2019-09-11 12:36:07 -0500 | [diff] [blame] | 18 | #include "data_interface.hpp" |
| 19 | |
Matt Spinler | cad9c2b | 2019-12-02 15:42:01 -0600 | [diff] [blame] | 20 | #include <fstream> |
Matt Spinler | a7d9d96 | 2019-11-06 15:01:25 -0600 | [diff] [blame] | 21 | #include <xyz/openbmc_project/State/OperatingSystem/Status/server.hpp> |
| 22 | |
Matt Spinler | c8705e2 | 2019-09-11 12:36:07 -0500 | [diff] [blame] | 23 | namespace openpower |
| 24 | { |
| 25 | namespace pels |
| 26 | { |
| 27 | |
| 28 | namespace service_name |
| 29 | { |
| 30 | constexpr auto objectMapper = "xyz.openbmc_project.ObjectMapper"; |
| 31 | } // namespace service_name |
| 32 | |
| 33 | namespace object_path |
| 34 | { |
| 35 | constexpr auto objectMapper = "/xyz/openbmc_project/object_mapper"; |
| 36 | constexpr auto systemInv = "/xyz/openbmc_project/inventory/system"; |
Matt Spinler | b3d488f | 2020-02-21 15:30:46 -0600 | [diff] [blame] | 37 | constexpr auto baseInv = "/xyz/openbmc_project/inventory"; |
Matt Spinler | 4aa23a1 | 2020-02-03 15:05:09 -0600 | [diff] [blame] | 38 | constexpr auto bmcState = "/xyz/openbmc_project/state/bmc0"; |
| 39 | constexpr auto chassisState = "/xyz/openbmc_project/state/chassis0"; |
Matt Spinler | a7d9d96 | 2019-11-06 15:01:25 -0600 | [diff] [blame] | 40 | constexpr auto hostState = "/xyz/openbmc_project/state/host0"; |
Matt Spinler | b3f5186 | 2019-12-09 13:55:10 -0600 | [diff] [blame] | 41 | constexpr auto pldm = "/xyz/openbmc_project/pldm"; |
Matt Spinler | 9cf3cfd | 2020-02-03 14:41:55 -0600 | [diff] [blame] | 42 | constexpr auto enableHostPELs = |
| 43 | "/xyz/openbmc_project/logging/send_event_logs_to_host"; |
Matt Spinler | c8705e2 | 2019-09-11 12:36:07 -0500 | [diff] [blame] | 44 | } // namespace object_path |
| 45 | |
| 46 | namespace interface |
| 47 | { |
| 48 | constexpr auto dbusProperty = "org.freedesktop.DBus.Properties"; |
| 49 | constexpr auto objectMapper = "xyz.openbmc_project.ObjectMapper"; |
| 50 | constexpr auto invAsset = "xyz.openbmc_project.Inventory.Decorator.Asset"; |
Matt Spinler | a7d9d96 | 2019-11-06 15:01:25 -0600 | [diff] [blame] | 51 | constexpr auto osStatus = "xyz.openbmc_project.State.OperatingSystem.Status"; |
Matt Spinler | b3f5186 | 2019-12-09 13:55:10 -0600 | [diff] [blame] | 52 | constexpr auto pldmRequester = "xyz.openbmc_project.PLDM.Requester"; |
Matt Spinler | 9cf3cfd | 2020-02-03 14:41:55 -0600 | [diff] [blame] | 53 | constexpr auto enable = "xyz.openbmc_project.Object.Enable"; |
Matt Spinler | 4aa23a1 | 2020-02-03 15:05:09 -0600 | [diff] [blame] | 54 | constexpr auto bmcState = "xyz.openbmc_project.State.BMC"; |
| 55 | constexpr auto chassisState = "xyz.openbmc_project.State.Chassis"; |
| 56 | constexpr auto hostState = "xyz.openbmc_project.State.Host"; |
Matt Spinler | b3d488f | 2020-02-21 15:30:46 -0600 | [diff] [blame] | 57 | constexpr auto invMotherboard = |
| 58 | "xyz.openbmc_project.Inventory.Item.Board.Motherboard"; |
| 59 | constexpr auto viniRecordVPD = "com.ibm.ipzvpd.VINI"; |
Matt Spinler | c8705e2 | 2019-09-11 12:36:07 -0500 | [diff] [blame] | 60 | } // namespace interface |
| 61 | |
Matt Spinler | a7d9d96 | 2019-11-06 15:01:25 -0600 | [diff] [blame] | 62 | using namespace sdbusplus::xyz::openbmc_project::State::OperatingSystem::server; |
Matt Spinler | b3d488f | 2020-02-21 15:30:46 -0600 | [diff] [blame] | 63 | using sdbusplus::exception::SdBusError; |
Matt Spinler | a7d9d96 | 2019-11-06 15:01:25 -0600 | [diff] [blame] | 64 | |
Matt Spinler | c8705e2 | 2019-09-11 12:36:07 -0500 | [diff] [blame] | 65 | DataInterface::DataInterface(sdbusplus::bus::bus& bus) : _bus(bus) |
| 66 | { |
Matt Spinler | cad9c2b | 2019-12-02 15:42:01 -0600 | [diff] [blame] | 67 | readBMCFWVersion(); |
| 68 | readServerFWVersion(); |
Matt Spinler | 677381b | 2020-01-23 10:04:29 -0600 | [diff] [blame] | 69 | readBMCFWVersionID(); |
Matt Spinler | b3d488f | 2020-02-21 15:30:46 -0600 | [diff] [blame] | 70 | readMotherboardCCIN(); |
Matt Spinler | 2a28c93 | 2020-02-03 14:23:40 -0600 | [diff] [blame] | 71 | |
| 72 | // Watch both the Model and SN properties on the system's Asset iface |
| 73 | _properties.emplace_back(std::make_unique<InterfaceWatcher<DataInterface>>( |
| 74 | bus, object_path::systemInv, interface::invAsset, *this, |
| 75 | [this](const auto& properties) { |
| 76 | auto model = properties.find("Model"); |
| 77 | if (model != properties.end()) |
| 78 | { |
| 79 | this->_machineTypeModel = std::get<std::string>(model->second); |
| 80 | } |
| 81 | |
| 82 | auto sn = properties.find("SerialNumber"); |
| 83 | if (sn != properties.end()) |
| 84 | { |
| 85 | this->_machineSerialNumber = std::get<std::string>(sn->second); |
| 86 | } |
| 87 | })); |
| 88 | |
| 89 | // Watch the OperatingSystemState property |
| 90 | _properties.emplace_back(std::make_unique<PropertyWatcher<DataInterface>>( |
| 91 | bus, object_path::hostState, interface::osStatus, |
| 92 | "OperatingSystemState", *this, [this](const auto& value) { |
| 93 | auto status = |
| 94 | Status::convertOSStatusFromString(std::get<std::string>(value)); |
| 95 | |
| 96 | if ((status == Status::OSStatus::BootComplete) || |
| 97 | (status == Status::OSStatus::Standby)) |
| 98 | { |
Matt Spinler | 4aa23a1 | 2020-02-03 15:05:09 -0600 | [diff] [blame] | 99 | setHostUp(true); |
Matt Spinler | 2a28c93 | 2020-02-03 14:23:40 -0600 | [diff] [blame] | 100 | } |
| 101 | else |
| 102 | { |
Matt Spinler | 4aa23a1 | 2020-02-03 15:05:09 -0600 | [diff] [blame] | 103 | setHostUp(false); |
Matt Spinler | 2a28c93 | 2020-02-03 14:23:40 -0600 | [diff] [blame] | 104 | } |
| 105 | })); |
Matt Spinler | 9cf3cfd | 2020-02-03 14:41:55 -0600 | [diff] [blame] | 106 | |
| 107 | // Watch the host PEL enable property |
| 108 | _properties.emplace_back(std::make_unique<PropertyWatcher<DataInterface>>( |
| 109 | bus, object_path::enableHostPELs, interface::enable, "Enabled", *this, |
| 110 | [this](const auto& value) { |
| 111 | this->_sendPELsToHost = std::get<bool>(value); |
| 112 | })); |
Matt Spinler | 4aa23a1 | 2020-02-03 15:05:09 -0600 | [diff] [blame] | 113 | |
| 114 | // Watch the BMCState property |
| 115 | _properties.emplace_back(std::make_unique<PropertyWatcher<DataInterface>>( |
| 116 | bus, object_path::bmcState, interface::bmcState, "CurrentBMCState", |
| 117 | *this, [this](const auto& value) { |
| 118 | this->_bmcState = std::get<std::string>(value); |
| 119 | })); |
| 120 | |
| 121 | // Watch the chassis current and requested power state properties |
| 122 | _properties.emplace_back(std::make_unique<InterfaceWatcher<DataInterface>>( |
| 123 | bus, object_path::chassisState, interface::chassisState, *this, |
| 124 | [this](const auto& properties) { |
| 125 | auto state = properties.find("CurrentPowerState"); |
| 126 | if (state != properties.end()) |
| 127 | { |
| 128 | this->_chassisState = std::get<std::string>(state->second); |
| 129 | } |
| 130 | |
| 131 | auto trans = properties.find("RequestedPowerTransition"); |
| 132 | if (trans != properties.end()) |
| 133 | { |
| 134 | this->_chassisTransition = std::get<std::string>(trans->second); |
| 135 | } |
| 136 | })); |
| 137 | |
| 138 | // Watch the CurrentHostState property |
| 139 | _properties.emplace_back(std::make_unique<PropertyWatcher<DataInterface>>( |
| 140 | bus, object_path::hostState, interface::hostState, "CurrentHostState", |
| 141 | *this, [this](const auto& value) { |
| 142 | this->_hostState = std::get<std::string>(value); |
| 143 | })); |
Matt Spinler | c8705e2 | 2019-09-11 12:36:07 -0500 | [diff] [blame] | 144 | } |
| 145 | |
Matt Spinler | 2a28c93 | 2020-02-03 14:23:40 -0600 | [diff] [blame] | 146 | DBusPropertyMap |
| 147 | DataInterface::getAllProperties(const std::string& service, |
| 148 | const std::string& objectPath, |
| 149 | const std::string& interface) const |
Matt Spinler | c8705e2 | 2019-09-11 12:36:07 -0500 | [diff] [blame] | 150 | { |
| 151 | DBusPropertyMap properties; |
| 152 | |
| 153 | auto method = _bus.new_method_call(service.c_str(), objectPath.c_str(), |
| 154 | interface::dbusProperty, "GetAll"); |
| 155 | method.append(interface); |
| 156 | auto reply = _bus.call(method); |
| 157 | |
| 158 | reply.read(properties); |
| 159 | |
| 160 | return properties; |
| 161 | } |
| 162 | |
Matt Spinler | a7d9d96 | 2019-11-06 15:01:25 -0600 | [diff] [blame] | 163 | void DataInterface::getProperty(const std::string& service, |
| 164 | const std::string& objectPath, |
| 165 | const std::string& interface, |
Matt Spinler | 2a28c93 | 2020-02-03 14:23:40 -0600 | [diff] [blame] | 166 | const std::string& property, |
| 167 | DBusValue& value) const |
Matt Spinler | a7d9d96 | 2019-11-06 15:01:25 -0600 | [diff] [blame] | 168 | { |
| 169 | |
| 170 | auto method = _bus.new_method_call(service.c_str(), objectPath.c_str(), |
| 171 | interface::dbusProperty, "Get"); |
| 172 | method.append(interface, property); |
| 173 | auto reply = _bus.call(method); |
| 174 | |
| 175 | reply.read(value); |
| 176 | } |
| 177 | |
Matt Spinler | b3d488f | 2020-02-21 15:30:46 -0600 | [diff] [blame] | 178 | DBusPathList DataInterface::getPaths(const DBusInterfaceList& interfaces) const |
| 179 | { |
| 180 | |
| 181 | auto method = _bus.new_method_call( |
| 182 | service_name::objectMapper, object_path::objectMapper, |
| 183 | interface::objectMapper, "GetSubTreePaths"); |
| 184 | |
| 185 | method.append(std::string{"/"}, 0, interfaces); |
| 186 | |
| 187 | auto reply = _bus.call(method); |
| 188 | |
| 189 | DBusPathList paths; |
| 190 | reply.read(paths); |
| 191 | |
| 192 | return paths; |
| 193 | } |
| 194 | |
Matt Spinler | c8705e2 | 2019-09-11 12:36:07 -0500 | [diff] [blame] | 195 | DBusService DataInterface::getService(const std::string& objectPath, |
Matt Spinler | b3f5186 | 2019-12-09 13:55:10 -0600 | [diff] [blame] | 196 | const std::string& interface) const |
Matt Spinler | c8705e2 | 2019-09-11 12:36:07 -0500 | [diff] [blame] | 197 | { |
| 198 | auto method = _bus.new_method_call(service_name::objectMapper, |
| 199 | object_path::objectMapper, |
| 200 | interface::objectMapper, "GetObject"); |
| 201 | |
| 202 | method.append(objectPath, std::vector<std::string>({interface})); |
| 203 | |
| 204 | auto reply = _bus.call(method); |
| 205 | |
| 206 | std::map<DBusService, DBusInterfaceList> response; |
| 207 | reply.read(response); |
| 208 | |
| 209 | if (!response.empty()) |
| 210 | { |
| 211 | return response.begin()->first; |
| 212 | } |
| 213 | |
| 214 | return std::string{}; |
| 215 | } |
Matt Spinler | a7d9d96 | 2019-11-06 15:01:25 -0600 | [diff] [blame] | 216 | |
Matt Spinler | 677381b | 2020-01-23 10:04:29 -0600 | [diff] [blame] | 217 | /** |
| 218 | * @brief Return a value found in the /etc/os-release file |
| 219 | * |
| 220 | * @param[in] key - The key name, like "VERSION" |
| 221 | * |
| 222 | * @return std::optional<std::string> - The value |
| 223 | */ |
| 224 | std::optional<std::string> getOSReleaseValue(const std::string& key) |
Matt Spinler | cad9c2b | 2019-12-02 15:42:01 -0600 | [diff] [blame] | 225 | { |
| 226 | std::ifstream versionFile{BMC_VERSION_FILE}; |
| 227 | std::string line; |
Matt Spinler | 677381b | 2020-01-23 10:04:29 -0600 | [diff] [blame] | 228 | std::string keyPattern{key + '='}; |
Matt Spinler | cad9c2b | 2019-12-02 15:42:01 -0600 | [diff] [blame] | 229 | |
| 230 | while (std::getline(versionFile, line)) |
| 231 | { |
Matt Spinler | 677381b | 2020-01-23 10:04:29 -0600 | [diff] [blame] | 232 | if (line.find(keyPattern) != std::string::npos) |
Matt Spinler | cad9c2b | 2019-12-02 15:42:01 -0600 | [diff] [blame] | 233 | { |
| 234 | auto pos = line.find_first_of('"') + 1; |
Matt Spinler | 677381b | 2020-01-23 10:04:29 -0600 | [diff] [blame] | 235 | auto value = line.substr(pos, line.find_last_of('"') - pos); |
| 236 | return value; |
Matt Spinler | cad9c2b | 2019-12-02 15:42:01 -0600 | [diff] [blame] | 237 | } |
| 238 | } |
Matt Spinler | 677381b | 2020-01-23 10:04:29 -0600 | [diff] [blame] | 239 | |
| 240 | return std::nullopt; |
| 241 | } |
| 242 | |
| 243 | void DataInterface::readBMCFWVersion() |
| 244 | { |
| 245 | _bmcFWVersion = getOSReleaseValue("VERSION").value_or(""); |
Matt Spinler | cad9c2b | 2019-12-02 15:42:01 -0600 | [diff] [blame] | 246 | } |
| 247 | |
| 248 | void DataInterface::readServerFWVersion() |
| 249 | { |
| 250 | // Not available yet |
| 251 | } |
| 252 | |
Matt Spinler | 677381b | 2020-01-23 10:04:29 -0600 | [diff] [blame] | 253 | void DataInterface::readBMCFWVersionID() |
| 254 | { |
| 255 | _bmcFWVersionID = getOSReleaseValue("VERSION_ID").value_or(""); |
| 256 | } |
| 257 | |
Matt Spinler | b3d488f | 2020-02-21 15:30:46 -0600 | [diff] [blame] | 258 | void DataInterface::readMotherboardCCIN() |
| 259 | { |
| 260 | try |
| 261 | { |
| 262 | // First, try to find the motherboard |
| 263 | auto motherboards = getPaths({interface::invMotherboard}); |
| 264 | if (motherboards.empty()) |
| 265 | { |
| 266 | throw std::runtime_error("No motherboards yet"); |
| 267 | } |
| 268 | |
| 269 | // Found it, so now get the CCIN |
| 270 | _properties.emplace_back( |
| 271 | std::make_unique<PropertyWatcher<DataInterface>>( |
| 272 | _bus, motherboards.front(), interface::viniRecordVPD, "CC", |
| 273 | *this, |
| 274 | [this](const auto& ccin) { this->setMotherboardCCIN(ccin); })); |
| 275 | } |
| 276 | catch (const std::exception& e) |
| 277 | { |
| 278 | // No motherboard in the inventory yet - watch for it |
| 279 | _inventoryIfacesAddedMatch = std::make_unique<sdbusplus::bus::match_t>( |
| 280 | _bus, match_rules::interfacesAdded(object_path::baseInv), |
| 281 | std::bind(std::mem_fn(&DataInterface::motherboardIfaceAdded), this, |
| 282 | std::placeholders::_1)); |
| 283 | } |
| 284 | } |
| 285 | |
| 286 | void DataInterface::motherboardIfaceAdded(sdbusplus::message::message& msg) |
| 287 | { |
| 288 | sdbusplus::message::object_path path; |
| 289 | DBusInterfaceMap interfaces; |
| 290 | |
| 291 | msg.read(path, interfaces); |
| 292 | |
| 293 | // This is watching the whole inventory, so check if it's what we want |
| 294 | if (interfaces.find(interface::invMotherboard) == interfaces.end()) |
| 295 | { |
| 296 | return; |
| 297 | } |
| 298 | |
| 299 | // Done watching for any new inventory interfaces |
| 300 | _inventoryIfacesAddedMatch.reset(); |
| 301 | |
| 302 | // Watch the motherboard CCIN, using the service from this signal |
| 303 | // for the initial property read. |
| 304 | _properties.emplace_back(std::make_unique<PropertyWatcher<DataInterface>>( |
| 305 | _bus, path, interface::viniRecordVPD, "CC", msg.get_sender(), *this, |
| 306 | [this](const auto& ccin) { this->setMotherboardCCIN(ccin); })); |
| 307 | } |
| 308 | |
Matt Spinler | c8705e2 | 2019-09-11 12:36:07 -0500 | [diff] [blame] | 309 | } // namespace pels |
| 310 | } // namespace openpower |