blob: f01bc8ea36010779990c9f4e30754e74437bbeab [file] [log] [blame]
Shawn McCarney452de222024-05-30 15:23:09 -05001/**
2 * Copyright © 2024 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
17#include "device_finder.hpp"
18
19#include <phosphor-logging/lg2.hpp>
20
21#include <exception>
22#include <format>
23#include <stdexcept>
24#include <utility>
25#include <variant>
26#include <vector>
27
28namespace phosphor::power::sequencer
29{
30
31const std::string deviceInterfacesService = "xyz.openbmc_project.EntityManager";
32
33const std::vector<std::string> deviceInterfaces{
34 "xyz.openbmc_project.Configuration.UCD90160",
35 "xyz.openbmc_project.Configuration.UCD90320"};
36
37const std::string typeProperty = "Type";
38const std::string nameProperty = "Name";
39const std::string busProperty = "Bus";
40const std::string addressProperty = "Address";
41
42DeviceFinder::DeviceFinder(sdbusplus::bus_t& bus, Callback callback) :
Shawn McCarney1838dbf2024-06-05 23:19:04 -050043 callback{std::move(callback)}
44{
45 interfacesFinder = std::make_unique<DBusInterfacesFinder>(
Shawn McCarney452de222024-05-30 15:23:09 -050046 bus, deviceInterfacesService, deviceInterfaces,
Shawn McCarney1838dbf2024-06-05 23:19:04 -050047 std::bind_front(&DeviceFinder::interfaceFoundCallback, this));
48}
Shawn McCarney452de222024-05-30 15:23:09 -050049
50void DeviceFinder::interfaceFoundCallback(
51 [[maybe_unused]] const std::string& path, const std::string& interface,
52 const DbusPropertyMap& properties)
53{
54 try
55 {
56 DeviceProperties device{};
57
58 auto value = getPropertyValue(properties, typeProperty);
59 device.type = std::get<std::string>(value);
60
61 value = getPropertyValue(properties, nameProperty);
62 device.name = std::get<std::string>(value);
63
64 value = getPropertyValue(properties, busProperty);
65 device.bus = static_cast<uint8_t>(std::get<uint64_t>(value));
66
67 value = getPropertyValue(properties, addressProperty);
68 device.address = static_cast<uint16_t>(std::get<uint64_t>(value));
69
70 callback(device);
71 }
72 catch (const std::exception& e)
73 {
74 lg2::error(
75 "Unable to obtain properties of interface {INTERFACE}: {ERROR}",
76 "INTERFACE", interface, "ERROR", e);
77 }
78}
79
Patrick Williamsf5402192024-08-16 15:20:53 -040080const DbusVariant& DeviceFinder::getPropertyValue(
81 const DbusPropertyMap& properties, const std::string& propertyName)
Shawn McCarney452de222024-05-30 15:23:09 -050082{
83 auto it = properties.find(propertyName);
84 if (it == properties.end())
85 {
86 throw std::runtime_error{
87 std::format("{} property not found", propertyName)};
88 }
89 return it->second;
90}
91
92} // namespace phosphor::power::sequencer