Jim Wright | 1553cd9 | 2021-03-31 16:11:59 -0500 | [diff] [blame] | 1 | /** |
| 2 | * Copyright © 2021 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 | */ |
| 16 | |
Jim Wright | 7945dd2 | 2021-04-06 16:55:15 -0500 | [diff] [blame] | 17 | #include "ucd90320_monitor.hpp" |
| 18 | |
Jim Wright | 56ae78e | 2021-12-01 14:46:15 -0600 | [diff] [blame] | 19 | #include "utility.hpp" |
Jim Wright | 7945dd2 | 2021-04-06 16:55:15 -0500 | [diff] [blame] | 20 | |
Jim Wright | 56ae78e | 2021-12-01 14:46:15 -0600 | [diff] [blame] | 21 | #include <fmt/format.h> |
| 22 | #include <fmt/ranges.h> |
| 23 | |
| 24 | #include <phosphor-logging/log.hpp> |
| 25 | #include <sdbusplus/bus.hpp> |
| 26 | |
| 27 | #include <map> |
Jim Wright | 7945dd2 | 2021-04-06 16:55:15 -0500 | [diff] [blame] | 28 | #include <string> |
| 29 | |
| 30 | namespace phosphor::power::sequencer |
Jim Wright | 1553cd9 | 2021-03-31 16:11:59 -0500 | [diff] [blame] | 31 | { |
Jim Wright | 7945dd2 | 2021-04-06 16:55:15 -0500 | [diff] [blame] | 32 | |
Jim Wright | 56ae78e | 2021-12-01 14:46:15 -0600 | [diff] [blame] | 33 | using namespace phosphor::logging; |
| 34 | using namespace phosphor::power; |
| 35 | |
| 36 | const std::string compatibleInterface = |
| 37 | "xyz.openbmc_project.Configuration.IBMCompatibleSystem"; |
| 38 | const std::string compatibleNamesProperty = "Names"; |
| 39 | |
Jim Wright | 7945dd2 | 2021-04-06 16:55:15 -0500 | [diff] [blame] | 40 | UCD90320Monitor::UCD90320Monitor(sdbusplus::bus::bus& bus, std::uint8_t i2cBus, |
| 41 | std::uint16_t i2cAddress) : |
| 42 | PowerSequencerMonitor(), |
Jim Wright | 56ae78e | 2021-12-01 14:46:15 -0600 | [diff] [blame] | 43 | bus{bus}, match{bus, |
| 44 | sdbusplus::bus::match::rules::interfacesAdded() + |
| 45 | sdbusplus::bus::match::rules::sender( |
| 46 | "xyz.openbmc_project.EntityManager"), |
| 47 | std::bind(&UCD90320Monitor::interfacesAddedHandler, this, |
| 48 | std::placeholders::_1)}, |
| 49 | pmbusInterface{ |
| 50 | fmt::format("/sys/bus/i2c/devices/{}-{:04x}", i2cBus, i2cAddress) |
| 51 | .c_str(), |
| 52 | "ucd9000", 0} |
| 53 | |
| 54 | { |
| 55 | // Use the compatible system types information, if already available, to |
| 56 | // load the configuration file |
| 57 | findCompatibleSystemTypes(); |
Jim Wright | 1553cd9 | 2021-03-31 16:11:59 -0500 | [diff] [blame] | 58 | } |
Jim Wright | 56ae78e | 2021-12-01 14:46:15 -0600 | [diff] [blame] | 59 | |
| 60 | void UCD90320Monitor::findCompatibleSystemTypes() |
| 61 | { |
| 62 | try |
| 63 | { |
| 64 | auto subTree = util::getSubTree(bus, "/xyz/openbmc_project/inventory", |
| 65 | compatibleInterface, 0); |
| 66 | |
| 67 | auto objectIt = subTree.cbegin(); |
| 68 | if (objectIt != subTree.cend()) |
| 69 | { |
| 70 | const auto& objPath = objectIt->first; |
| 71 | |
| 72 | // Get the first service name |
| 73 | auto serviceIt = objectIt->second.cbegin(); |
| 74 | if (serviceIt != objectIt->second.cend()) |
| 75 | { |
| 76 | std::string service = serviceIt->first; |
| 77 | if (!service.empty()) |
| 78 | { |
| 79 | std::vector<std::string> compatibleSystemTypes; |
| 80 | |
| 81 | // Get compatible system types property value |
| 82 | util::getProperty(compatibleInterface, |
| 83 | compatibleNamesProperty, objPath, service, |
| 84 | bus, compatibleSystemTypes); |
| 85 | |
| 86 | log<level::DEBUG>( |
| 87 | fmt::format("Found compatible systems: {}", |
| 88 | compatibleSystemTypes) |
| 89 | .c_str()); |
| 90 | // Use compatible systems information to find config file |
| 91 | } |
| 92 | } |
| 93 | } |
| 94 | } |
| 95 | catch (const std::exception&) |
| 96 | { |
| 97 | // Compatible system types information is not available. |
| 98 | } |
| 99 | } |
| 100 | |
| 101 | void UCD90320Monitor::interfacesAddedHandler(sdbusplus::message::message& msg) |
| 102 | { |
| 103 | // Verify message is valid |
| 104 | if (!msg) |
| 105 | { |
| 106 | return; |
| 107 | } |
| 108 | |
| 109 | try |
| 110 | { |
| 111 | // Read the dbus message |
| 112 | sdbusplus::message::object_path objPath; |
| 113 | std::map<std::string, |
| 114 | std::map<std::string, std::variant<std::vector<std::string>>>> |
| 115 | interfaces; |
| 116 | msg.read(objPath, interfaces); |
| 117 | |
| 118 | // Find the compatible interface, if present |
| 119 | auto itIntf = interfaces.find(compatibleInterface); |
| 120 | if (itIntf != interfaces.cend()) |
| 121 | { |
| 122 | // Find the Names property of the compatible interface, if present |
| 123 | auto itProp = itIntf->second.find(compatibleNamesProperty); |
| 124 | if (itProp != itIntf->second.cend()) |
| 125 | { |
| 126 | // Get value of Names property |
| 127 | const auto& propValue = std::get<0>(itProp->second); |
| 128 | if (!propValue.empty()) |
| 129 | { |
| 130 | log<level::INFO>( |
| 131 | fmt::format( |
| 132 | "InterfacesAdded for compatible systems: {}", |
| 133 | propValue) |
| 134 | .c_str()); |
| 135 | |
| 136 | // Use compatible systems information to find config file |
| 137 | } |
| 138 | } |
| 139 | } |
| 140 | } |
| 141 | catch (const std::exception&) |
| 142 | { |
| 143 | // Error trying to read interfacesAdded message. |
| 144 | } |
| 145 | } |
Jim Wright | 7945dd2 | 2021-04-06 16:55:15 -0500 | [diff] [blame] | 146 | |
| 147 | } // namespace phosphor::power::sequencer |