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 | 4aa23a1 | 2020-02-03 15:05:09 -0600 | [diff] [blame^] | 37 | constexpr auto bmcState = "/xyz/openbmc_project/state/bmc0"; |
| 38 | constexpr auto chassisState = "/xyz/openbmc_project/state/chassis0"; |
Matt Spinler | a7d9d96 | 2019-11-06 15:01:25 -0600 | [diff] [blame] | 39 | constexpr auto hostState = "/xyz/openbmc_project/state/host0"; |
Matt Spinler | b3f5186 | 2019-12-09 13:55:10 -0600 | [diff] [blame] | 40 | constexpr auto pldm = "/xyz/openbmc_project/pldm"; |
Matt Spinler | 9cf3cfd | 2020-02-03 14:41:55 -0600 | [diff] [blame] | 41 | constexpr auto enableHostPELs = |
| 42 | "/xyz/openbmc_project/logging/send_event_logs_to_host"; |
Matt Spinler | c8705e2 | 2019-09-11 12:36:07 -0500 | [diff] [blame] | 43 | } // namespace object_path |
| 44 | |
| 45 | namespace interface |
| 46 | { |
| 47 | constexpr auto dbusProperty = "org.freedesktop.DBus.Properties"; |
| 48 | constexpr auto objectMapper = "xyz.openbmc_project.ObjectMapper"; |
| 49 | constexpr auto invAsset = "xyz.openbmc_project.Inventory.Decorator.Asset"; |
Matt Spinler | a7d9d96 | 2019-11-06 15:01:25 -0600 | [diff] [blame] | 50 | constexpr auto osStatus = "xyz.openbmc_project.State.OperatingSystem.Status"; |
Matt Spinler | b3f5186 | 2019-12-09 13:55:10 -0600 | [diff] [blame] | 51 | constexpr auto pldmRequester = "xyz.openbmc_project.PLDM.Requester"; |
Matt Spinler | 9cf3cfd | 2020-02-03 14:41:55 -0600 | [diff] [blame] | 52 | constexpr auto enable = "xyz.openbmc_project.Object.Enable"; |
Matt Spinler | 4aa23a1 | 2020-02-03 15:05:09 -0600 | [diff] [blame^] | 53 | constexpr auto bmcState = "xyz.openbmc_project.State.BMC"; |
| 54 | constexpr auto chassisState = "xyz.openbmc_project.State.Chassis"; |
| 55 | constexpr auto hostState = "xyz.openbmc_project.State.Host"; |
Matt Spinler | c8705e2 | 2019-09-11 12:36:07 -0500 | [diff] [blame] | 56 | } // namespace interface |
| 57 | |
Matt Spinler | a7d9d96 | 2019-11-06 15:01:25 -0600 | [diff] [blame] | 58 | using namespace sdbusplus::xyz::openbmc_project::State::OperatingSystem::server; |
| 59 | |
Matt Spinler | c8705e2 | 2019-09-11 12:36:07 -0500 | [diff] [blame] | 60 | DataInterface::DataInterface(sdbusplus::bus::bus& bus) : _bus(bus) |
| 61 | { |
Matt Spinler | cad9c2b | 2019-12-02 15:42:01 -0600 | [diff] [blame] | 62 | readBMCFWVersion(); |
| 63 | readServerFWVersion(); |
Matt Spinler | 677381b | 2020-01-23 10:04:29 -0600 | [diff] [blame] | 64 | readBMCFWVersionID(); |
Matt Spinler | 2a28c93 | 2020-02-03 14:23:40 -0600 | [diff] [blame] | 65 | |
| 66 | // Watch both the Model and SN properties on the system's Asset iface |
| 67 | _properties.emplace_back(std::make_unique<InterfaceWatcher<DataInterface>>( |
| 68 | bus, object_path::systemInv, interface::invAsset, *this, |
| 69 | [this](const auto& properties) { |
| 70 | auto model = properties.find("Model"); |
| 71 | if (model != properties.end()) |
| 72 | { |
| 73 | this->_machineTypeModel = std::get<std::string>(model->second); |
| 74 | } |
| 75 | |
| 76 | auto sn = properties.find("SerialNumber"); |
| 77 | if (sn != properties.end()) |
| 78 | { |
| 79 | this->_machineSerialNumber = std::get<std::string>(sn->second); |
| 80 | } |
| 81 | })); |
| 82 | |
| 83 | // Watch the OperatingSystemState property |
| 84 | _properties.emplace_back(std::make_unique<PropertyWatcher<DataInterface>>( |
| 85 | bus, object_path::hostState, interface::osStatus, |
| 86 | "OperatingSystemState", *this, [this](const auto& value) { |
| 87 | auto status = |
| 88 | Status::convertOSStatusFromString(std::get<std::string>(value)); |
| 89 | |
| 90 | if ((status == Status::OSStatus::BootComplete) || |
| 91 | (status == Status::OSStatus::Standby)) |
| 92 | { |
Matt Spinler | 4aa23a1 | 2020-02-03 15:05:09 -0600 | [diff] [blame^] | 93 | setHostUp(true); |
Matt Spinler | 2a28c93 | 2020-02-03 14:23:40 -0600 | [diff] [blame] | 94 | } |
| 95 | else |
| 96 | { |
Matt Spinler | 4aa23a1 | 2020-02-03 15:05:09 -0600 | [diff] [blame^] | 97 | setHostUp(false); |
Matt Spinler | 2a28c93 | 2020-02-03 14:23:40 -0600 | [diff] [blame] | 98 | } |
| 99 | })); |
Matt Spinler | 9cf3cfd | 2020-02-03 14:41:55 -0600 | [diff] [blame] | 100 | |
| 101 | // Watch the host PEL enable property |
| 102 | _properties.emplace_back(std::make_unique<PropertyWatcher<DataInterface>>( |
| 103 | bus, object_path::enableHostPELs, interface::enable, "Enabled", *this, |
| 104 | [this](const auto& value) { |
| 105 | this->_sendPELsToHost = std::get<bool>(value); |
| 106 | })); |
Matt Spinler | 4aa23a1 | 2020-02-03 15:05:09 -0600 | [diff] [blame^] | 107 | |
| 108 | // Watch the BMCState property |
| 109 | _properties.emplace_back(std::make_unique<PropertyWatcher<DataInterface>>( |
| 110 | bus, object_path::bmcState, interface::bmcState, "CurrentBMCState", |
| 111 | *this, [this](const auto& value) { |
| 112 | this->_bmcState = std::get<std::string>(value); |
| 113 | })); |
| 114 | |
| 115 | // Watch the chassis current and requested power state properties |
| 116 | _properties.emplace_back(std::make_unique<InterfaceWatcher<DataInterface>>( |
| 117 | bus, object_path::chassisState, interface::chassisState, *this, |
| 118 | [this](const auto& properties) { |
| 119 | auto state = properties.find("CurrentPowerState"); |
| 120 | if (state != properties.end()) |
| 121 | { |
| 122 | this->_chassisState = std::get<std::string>(state->second); |
| 123 | } |
| 124 | |
| 125 | auto trans = properties.find("RequestedPowerTransition"); |
| 126 | if (trans != properties.end()) |
| 127 | { |
| 128 | this->_chassisTransition = std::get<std::string>(trans->second); |
| 129 | } |
| 130 | })); |
| 131 | |
| 132 | // Watch the CurrentHostState property |
| 133 | _properties.emplace_back(std::make_unique<PropertyWatcher<DataInterface>>( |
| 134 | bus, object_path::hostState, interface::hostState, "CurrentHostState", |
| 135 | *this, [this](const auto& value) { |
| 136 | this->_hostState = std::get<std::string>(value); |
| 137 | })); |
Matt Spinler | c8705e2 | 2019-09-11 12:36:07 -0500 | [diff] [blame] | 138 | } |
| 139 | |
Matt Spinler | 2a28c93 | 2020-02-03 14:23:40 -0600 | [diff] [blame] | 140 | DBusPropertyMap |
| 141 | DataInterface::getAllProperties(const std::string& service, |
| 142 | const std::string& objectPath, |
| 143 | const std::string& interface) const |
Matt Spinler | c8705e2 | 2019-09-11 12:36:07 -0500 | [diff] [blame] | 144 | { |
| 145 | DBusPropertyMap properties; |
| 146 | |
| 147 | auto method = _bus.new_method_call(service.c_str(), objectPath.c_str(), |
| 148 | interface::dbusProperty, "GetAll"); |
| 149 | method.append(interface); |
| 150 | auto reply = _bus.call(method); |
| 151 | |
| 152 | reply.read(properties); |
| 153 | |
| 154 | return properties; |
| 155 | } |
| 156 | |
Matt Spinler | a7d9d96 | 2019-11-06 15:01:25 -0600 | [diff] [blame] | 157 | void DataInterface::getProperty(const std::string& service, |
| 158 | const std::string& objectPath, |
| 159 | const std::string& interface, |
Matt Spinler | 2a28c93 | 2020-02-03 14:23:40 -0600 | [diff] [blame] | 160 | const std::string& property, |
| 161 | DBusValue& value) const |
Matt Spinler | a7d9d96 | 2019-11-06 15:01:25 -0600 | [diff] [blame] | 162 | { |
| 163 | |
| 164 | auto method = _bus.new_method_call(service.c_str(), objectPath.c_str(), |
| 165 | interface::dbusProperty, "Get"); |
| 166 | method.append(interface, property); |
| 167 | auto reply = _bus.call(method); |
| 168 | |
| 169 | reply.read(value); |
| 170 | } |
| 171 | |
Matt Spinler | c8705e2 | 2019-09-11 12:36:07 -0500 | [diff] [blame] | 172 | DBusService DataInterface::getService(const std::string& objectPath, |
Matt Spinler | b3f5186 | 2019-12-09 13:55:10 -0600 | [diff] [blame] | 173 | const std::string& interface) const |
Matt Spinler | c8705e2 | 2019-09-11 12:36:07 -0500 | [diff] [blame] | 174 | { |
| 175 | auto method = _bus.new_method_call(service_name::objectMapper, |
| 176 | object_path::objectMapper, |
| 177 | interface::objectMapper, "GetObject"); |
| 178 | |
| 179 | method.append(objectPath, std::vector<std::string>({interface})); |
| 180 | |
| 181 | auto reply = _bus.call(method); |
| 182 | |
| 183 | std::map<DBusService, DBusInterfaceList> response; |
| 184 | reply.read(response); |
| 185 | |
| 186 | if (!response.empty()) |
| 187 | { |
| 188 | return response.begin()->first; |
| 189 | } |
| 190 | |
| 191 | return std::string{}; |
| 192 | } |
Matt Spinler | a7d9d96 | 2019-11-06 15:01:25 -0600 | [diff] [blame] | 193 | |
Matt Spinler | b3f5186 | 2019-12-09 13:55:10 -0600 | [diff] [blame] | 194 | uint8_t DataInterface::getPLDMInstanceID(uint8_t eid) const |
| 195 | { |
| 196 | return 0; |
| 197 | // Don't use until PLDM switches to async D-Bus. |
| 198 | #if 0 |
| 199 | auto service = getService(object_path::pldm, interface::pldmRequester); |
| 200 | |
| 201 | auto method = |
| 202 | _bus.new_method_call(service.c_str(), object_path::pldm, |
| 203 | interface::pldmRequester, "GetInstanceId"); |
| 204 | method.append(eid); |
| 205 | auto reply = _bus.call(method); |
| 206 | |
| 207 | uint8_t instanceID = 0; |
| 208 | reply.read(instanceID); |
| 209 | |
| 210 | return instanceID; |
| 211 | #endif |
| 212 | } |
| 213 | |
Matt Spinler | 677381b | 2020-01-23 10:04:29 -0600 | [diff] [blame] | 214 | /** |
| 215 | * @brief Return a value found in the /etc/os-release file |
| 216 | * |
| 217 | * @param[in] key - The key name, like "VERSION" |
| 218 | * |
| 219 | * @return std::optional<std::string> - The value |
| 220 | */ |
| 221 | std::optional<std::string> getOSReleaseValue(const std::string& key) |
Matt Spinler | cad9c2b | 2019-12-02 15:42:01 -0600 | [diff] [blame] | 222 | { |
| 223 | std::ifstream versionFile{BMC_VERSION_FILE}; |
| 224 | std::string line; |
Matt Spinler | 677381b | 2020-01-23 10:04:29 -0600 | [diff] [blame] | 225 | std::string keyPattern{key + '='}; |
Matt Spinler | cad9c2b | 2019-12-02 15:42:01 -0600 | [diff] [blame] | 226 | |
| 227 | while (std::getline(versionFile, line)) |
| 228 | { |
Matt Spinler | 677381b | 2020-01-23 10:04:29 -0600 | [diff] [blame] | 229 | if (line.find(keyPattern) != std::string::npos) |
Matt Spinler | cad9c2b | 2019-12-02 15:42:01 -0600 | [diff] [blame] | 230 | { |
| 231 | auto pos = line.find_first_of('"') + 1; |
Matt Spinler | 677381b | 2020-01-23 10:04:29 -0600 | [diff] [blame] | 232 | auto value = line.substr(pos, line.find_last_of('"') - pos); |
| 233 | return value; |
Matt Spinler | cad9c2b | 2019-12-02 15:42:01 -0600 | [diff] [blame] | 234 | } |
| 235 | } |
Matt Spinler | 677381b | 2020-01-23 10:04:29 -0600 | [diff] [blame] | 236 | |
| 237 | return std::nullopt; |
| 238 | } |
| 239 | |
| 240 | void DataInterface::readBMCFWVersion() |
| 241 | { |
| 242 | _bmcFWVersion = getOSReleaseValue("VERSION").value_or(""); |
Matt Spinler | cad9c2b | 2019-12-02 15:42:01 -0600 | [diff] [blame] | 243 | } |
| 244 | |
| 245 | void DataInterface::readServerFWVersion() |
| 246 | { |
| 247 | // Not available yet |
| 248 | } |
| 249 | |
Matt Spinler | 677381b | 2020-01-23 10:04:29 -0600 | [diff] [blame] | 250 | void DataInterface::readBMCFWVersionID() |
| 251 | { |
| 252 | _bmcFWVersionID = getOSReleaseValue("VERSION_ID").value_or(""); |
| 253 | } |
| 254 | |
Matt Spinler | c8705e2 | 2019-09-11 12:36:07 -0500 | [diff] [blame] | 255 | } // namespace pels |
| 256 | } // namespace openpower |