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 | |
| 18 | namespace openpower |
| 19 | { |
| 20 | namespace pels |
| 21 | { |
| 22 | |
| 23 | namespace service_name |
| 24 | { |
| 25 | constexpr auto objectMapper = "xyz.openbmc_project.ObjectMapper"; |
| 26 | } // namespace service_name |
| 27 | |
| 28 | namespace object_path |
| 29 | { |
| 30 | constexpr auto objectMapper = "/xyz/openbmc_project/object_mapper"; |
| 31 | constexpr auto systemInv = "/xyz/openbmc_project/inventory/system"; |
| 32 | } // namespace object_path |
| 33 | |
| 34 | namespace interface |
| 35 | { |
| 36 | constexpr auto dbusProperty = "org.freedesktop.DBus.Properties"; |
| 37 | constexpr auto objectMapper = "xyz.openbmc_project.ObjectMapper"; |
| 38 | constexpr auto invAsset = "xyz.openbmc_project.Inventory.Decorator.Asset"; |
| 39 | } // namespace interface |
| 40 | |
| 41 | DataInterface::DataInterface(sdbusplus::bus::bus& bus) : _bus(bus) |
| 42 | { |
| 43 | readMTMS(); |
| 44 | } |
| 45 | |
| 46 | void DataInterface::readMTMS() |
| 47 | { |
| 48 | // If this runs when the inventory service isn't running, it will get the |
| 49 | // value whenever it starts via the propertiesChanged callback. |
| 50 | try |
| 51 | { |
| 52 | auto inventoryService = |
| 53 | getService(object_path::systemInv, interface::invAsset); |
| 54 | |
| 55 | if (!inventoryService.empty()) |
| 56 | { |
| 57 | auto properties = getAllProperties( |
| 58 | inventoryService, object_path::systemInv, interface::invAsset); |
| 59 | |
| 60 | _machineTypeModel = std::get<std::string>(properties["Model"]); |
| 61 | |
| 62 | _machineSerialNumber = |
| 63 | std::get<std::string>(properties["SerialNumber"]); |
| 64 | } |
| 65 | } |
| 66 | catch (std::exception& e) |
| 67 | { |
| 68 | // Inventory must not be running at this moment. |
| 69 | } |
| 70 | |
| 71 | // Keep up to date by watching for the propertiesChanged signal. |
| 72 | _sysInventoryPropMatch = std::make_unique<sdbusplus::bus::match_t>( |
| 73 | _bus, |
| 74 | sdbusplus::bus::match::rules::propertiesChanged(object_path::systemInv, |
| 75 | interface::invAsset), |
| 76 | std::bind(std::mem_fn(&DataInterface::sysAssetPropChanged), this, |
| 77 | std::placeholders::_1)); |
| 78 | } |
| 79 | |
| 80 | void DataInterface::sysAssetPropChanged(sdbusplus::message::message& msg) |
| 81 | { |
| 82 | DBusInterface interface; |
| 83 | DBusPropertyMap properties; |
| 84 | |
| 85 | msg.read(interface, properties); |
| 86 | |
| 87 | auto model = properties.find("Model"); |
| 88 | if (model != properties.end()) |
| 89 | { |
| 90 | _machineTypeModel = std::get<std::string>(model->second); |
| 91 | } |
| 92 | |
| 93 | auto sn = properties.find("SerialNumber"); |
| 94 | if (sn != properties.end()) |
| 95 | { |
| 96 | _machineSerialNumber = std::get<std::string>(sn->second); |
| 97 | } |
| 98 | } |
| 99 | |
| 100 | DBusPropertyMap DataInterface::getAllProperties(const std::string& service, |
| 101 | const std::string& objectPath, |
| 102 | const std::string& interface) |
| 103 | { |
| 104 | DBusPropertyMap properties; |
| 105 | |
| 106 | auto method = _bus.new_method_call(service.c_str(), objectPath.c_str(), |
| 107 | interface::dbusProperty, "GetAll"); |
| 108 | method.append(interface); |
| 109 | auto reply = _bus.call(method); |
| 110 | |
| 111 | reply.read(properties); |
| 112 | |
| 113 | return properties; |
| 114 | } |
| 115 | |
| 116 | DBusService DataInterface::getService(const std::string& objectPath, |
| 117 | const std::string& interface) |
| 118 | { |
| 119 | auto method = _bus.new_method_call(service_name::objectMapper, |
| 120 | object_path::objectMapper, |
| 121 | interface::objectMapper, "GetObject"); |
| 122 | |
| 123 | method.append(objectPath, std::vector<std::string>({interface})); |
| 124 | |
| 125 | auto reply = _bus.call(method); |
| 126 | |
| 127 | std::map<DBusService, DBusInterfaceList> response; |
| 128 | reply.read(response); |
| 129 | |
| 130 | if (!response.empty()) |
| 131 | { |
| 132 | return response.begin()->first; |
| 133 | } |
| 134 | |
| 135 | return std::string{}; |
| 136 | } |
| 137 | } // namespace pels |
| 138 | } // namespace openpower |