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