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 | c8705e2 | 2019-09-11 12:36:07 -0500 | [diff] [blame] | 16 | #include "data_interface.hpp" |
| 17 | |
Matt Spinler | a7d9d96 | 2019-11-06 15:01:25 -0600 | [diff] [blame] | 18 | #include <xyz/openbmc_project/State/OperatingSystem/Status/server.hpp> |
| 19 | |
Matt Spinler | c8705e2 | 2019-09-11 12:36:07 -0500 | [diff] [blame] | 20 | namespace openpower |
| 21 | { |
| 22 | namespace pels |
| 23 | { |
| 24 | |
| 25 | namespace service_name |
| 26 | { |
| 27 | constexpr auto objectMapper = "xyz.openbmc_project.ObjectMapper"; |
| 28 | } // namespace service_name |
| 29 | |
| 30 | namespace object_path |
| 31 | { |
| 32 | constexpr auto objectMapper = "/xyz/openbmc_project/object_mapper"; |
| 33 | constexpr auto systemInv = "/xyz/openbmc_project/inventory/system"; |
Matt Spinler | a7d9d96 | 2019-11-06 15:01:25 -0600 | [diff] [blame] | 34 | constexpr auto hostState = "/xyz/openbmc_project/state/host0"; |
Matt Spinler | c8705e2 | 2019-09-11 12:36:07 -0500 | [diff] [blame] | 35 | } // namespace object_path |
| 36 | |
| 37 | namespace interface |
| 38 | { |
| 39 | constexpr auto dbusProperty = "org.freedesktop.DBus.Properties"; |
| 40 | constexpr auto objectMapper = "xyz.openbmc_project.ObjectMapper"; |
| 41 | constexpr auto invAsset = "xyz.openbmc_project.Inventory.Decorator.Asset"; |
Matt Spinler | a7d9d96 | 2019-11-06 15:01:25 -0600 | [diff] [blame] | 42 | constexpr auto osStatus = "xyz.openbmc_project.State.OperatingSystem.Status"; |
Matt Spinler | c8705e2 | 2019-09-11 12:36:07 -0500 | [diff] [blame] | 43 | } // namespace interface |
| 44 | |
Matt Spinler | a7d9d96 | 2019-11-06 15:01:25 -0600 | [diff] [blame] | 45 | using namespace sdbusplus::xyz::openbmc_project::State::OperatingSystem::server; |
| 46 | |
Matt Spinler | c8705e2 | 2019-09-11 12:36:07 -0500 | [diff] [blame] | 47 | DataInterface::DataInterface(sdbusplus::bus::bus& bus) : _bus(bus) |
| 48 | { |
| 49 | readMTMS(); |
Matt Spinler | a7d9d96 | 2019-11-06 15:01:25 -0600 | [diff] [blame] | 50 | readHostState(); |
Matt Spinler | c8705e2 | 2019-09-11 12:36:07 -0500 | [diff] [blame] | 51 | } |
| 52 | |
| 53 | void DataInterface::readMTMS() |
| 54 | { |
| 55 | // If this runs when the inventory service isn't running, it will get the |
| 56 | // value whenever it starts via the propertiesChanged callback. |
| 57 | try |
| 58 | { |
| 59 | auto inventoryService = |
| 60 | getService(object_path::systemInv, interface::invAsset); |
| 61 | |
| 62 | if (!inventoryService.empty()) |
| 63 | { |
| 64 | auto properties = getAllProperties( |
| 65 | inventoryService, object_path::systemInv, interface::invAsset); |
| 66 | |
| 67 | _machineTypeModel = std::get<std::string>(properties["Model"]); |
| 68 | |
| 69 | _machineSerialNumber = |
| 70 | std::get<std::string>(properties["SerialNumber"]); |
| 71 | } |
| 72 | } |
| 73 | catch (std::exception& e) |
| 74 | { |
| 75 | // Inventory must not be running at this moment. |
| 76 | } |
| 77 | |
| 78 | // Keep up to date by watching for the propertiesChanged signal. |
| 79 | _sysInventoryPropMatch = std::make_unique<sdbusplus::bus::match_t>( |
| 80 | _bus, |
| 81 | sdbusplus::bus::match::rules::propertiesChanged(object_path::systemInv, |
| 82 | interface::invAsset), |
| 83 | std::bind(std::mem_fn(&DataInterface::sysAssetPropChanged), this, |
| 84 | std::placeholders::_1)); |
| 85 | } |
| 86 | |
| 87 | void DataInterface::sysAssetPropChanged(sdbusplus::message::message& msg) |
| 88 | { |
| 89 | DBusInterface interface; |
| 90 | DBusPropertyMap properties; |
| 91 | |
| 92 | msg.read(interface, properties); |
| 93 | |
| 94 | auto model = properties.find("Model"); |
| 95 | if (model != properties.end()) |
| 96 | { |
| 97 | _machineTypeModel = std::get<std::string>(model->second); |
| 98 | } |
| 99 | |
| 100 | auto sn = properties.find("SerialNumber"); |
| 101 | if (sn != properties.end()) |
| 102 | { |
| 103 | _machineSerialNumber = std::get<std::string>(sn->second); |
| 104 | } |
| 105 | } |
| 106 | |
| 107 | DBusPropertyMap DataInterface::getAllProperties(const std::string& service, |
| 108 | const std::string& objectPath, |
| 109 | const std::string& interface) |
| 110 | { |
| 111 | DBusPropertyMap properties; |
| 112 | |
| 113 | auto method = _bus.new_method_call(service.c_str(), objectPath.c_str(), |
| 114 | interface::dbusProperty, "GetAll"); |
| 115 | method.append(interface); |
| 116 | auto reply = _bus.call(method); |
| 117 | |
| 118 | reply.read(properties); |
| 119 | |
| 120 | return properties; |
| 121 | } |
| 122 | |
Matt Spinler | a7d9d96 | 2019-11-06 15:01:25 -0600 | [diff] [blame] | 123 | void DataInterface::getProperty(const std::string& service, |
| 124 | const std::string& objectPath, |
| 125 | const std::string& interface, |
| 126 | const std::string& property, DBusValue& value) |
| 127 | { |
| 128 | |
| 129 | auto method = _bus.new_method_call(service.c_str(), objectPath.c_str(), |
| 130 | interface::dbusProperty, "Get"); |
| 131 | method.append(interface, property); |
| 132 | auto reply = _bus.call(method); |
| 133 | |
| 134 | reply.read(value); |
| 135 | } |
| 136 | |
Matt Spinler | c8705e2 | 2019-09-11 12:36:07 -0500 | [diff] [blame] | 137 | DBusService DataInterface::getService(const std::string& objectPath, |
| 138 | const std::string& interface) |
| 139 | { |
| 140 | auto method = _bus.new_method_call(service_name::objectMapper, |
| 141 | object_path::objectMapper, |
| 142 | interface::objectMapper, "GetObject"); |
| 143 | |
| 144 | method.append(objectPath, std::vector<std::string>({interface})); |
| 145 | |
| 146 | auto reply = _bus.call(method); |
| 147 | |
| 148 | std::map<DBusService, DBusInterfaceList> response; |
| 149 | reply.read(response); |
| 150 | |
| 151 | if (!response.empty()) |
| 152 | { |
| 153 | return response.begin()->first; |
| 154 | } |
| 155 | |
| 156 | return std::string{}; |
| 157 | } |
Matt Spinler | a7d9d96 | 2019-11-06 15:01:25 -0600 | [diff] [blame] | 158 | |
| 159 | void DataInterface::readHostState() |
| 160 | { |
| 161 | _hostUp = false; |
| 162 | |
| 163 | try |
| 164 | { |
| 165 | auto service = getService(object_path::hostState, interface::osStatus); |
| 166 | if (!service.empty()) |
| 167 | { |
| 168 | DBusValue value; |
| 169 | getProperty(service, object_path::hostState, interface::osStatus, |
| 170 | "OperatingSystemState", value); |
| 171 | |
| 172 | auto status = |
| 173 | Status::convertOSStatusFromString(std::get<std::string>(value)); |
| 174 | |
| 175 | if ((status == Status::OSStatus::BootComplete) || |
| 176 | (status == Status::OSStatus::Standby)) |
| 177 | { |
| 178 | _hostUp = true; |
| 179 | } |
| 180 | } |
| 181 | } |
| 182 | catch (std::exception& e) |
| 183 | { |
| 184 | // Not available yet. |
| 185 | } |
| 186 | |
| 187 | // Keep up to date by watching for the propertiesChanged signal. |
| 188 | _osStateMatch = std::make_unique<sdbusplus::bus::match_t>( |
| 189 | _bus, |
| 190 | sdbusplus::bus::match::rules::propertiesChanged(object_path::hostState, |
| 191 | interface::osStatus), |
| 192 | std::bind(std::mem_fn(&DataInterface::osStatePropChanged), this, |
| 193 | std::placeholders::_1)); |
| 194 | } |
| 195 | |
| 196 | void DataInterface::osStatePropChanged(sdbusplus::message::message& msg) |
| 197 | { |
| 198 | DBusInterface interface; |
| 199 | DBusPropertyMap properties; |
| 200 | |
| 201 | msg.read(interface, properties); |
| 202 | |
| 203 | auto state = properties.find("OperatingSystemState"); |
| 204 | if (state != properties.end()) |
| 205 | { |
| 206 | auto status = Status::convertOSStatusFromString( |
| 207 | std::get<std::string>(state->second)); |
| 208 | |
| 209 | bool newHostState = false; |
| 210 | if ((status == Status::OSStatus::BootComplete) || |
| 211 | (status == Status::OSStatus::Standby)) |
| 212 | { |
| 213 | newHostState = true; |
| 214 | } |
| 215 | |
| 216 | setHostState(newHostState); |
| 217 | } |
| 218 | } |
| 219 | |
Matt Spinler | c8705e2 | 2019-09-11 12:36:07 -0500 | [diff] [blame] | 220 | } // namespace pels |
| 221 | } // namespace openpower |