xref: /openbmc/dbus-sensors/src/EntityManagerInterface.cpp (revision 0d2f96f1e95a6a37f00a3c458c8562d6af92e4a4)
115dde864SJagpal Singh Gill #include "EntityManagerInterface.hpp"
215dde864SJagpal Singh Gill 
315dde864SJagpal Singh Gill #include "Utils.hpp"
415dde864SJagpal Singh Gill 
515dde864SJagpal Singh Gill #include <phosphor-logging/lg2.hpp>
615dde864SJagpal Singh Gill #include <sdbusplus/async.hpp>
715dde864SJagpal Singh Gill #include <sdbusplus/message/native_types.hpp>
815dde864SJagpal Singh Gill #include <xyz/openbmc_project/Inventory/Item/client.hpp>
915dde864SJagpal Singh Gill 
1015dde864SJagpal Singh Gill #include <algorithm>
1115dde864SJagpal Singh Gill #include <utility>
1215dde864SJagpal Singh Gill 
1315dde864SJagpal Singh Gill namespace entity_manager
1415dde864SJagpal Singh Gill {
1515dde864SJagpal Singh Gill 
1615dde864SJagpal Singh Gill PHOSPHOR_LOG2_USING;
1715dde864SJagpal Singh Gill 
1815dde864SJagpal Singh Gill namespace rules_intf = sdbusplus::bus::match::rules;
1915dde864SJagpal Singh Gill 
EntityManagerInterface(sdbusplus::async::context & ctx,const interface_list_t & interfaceNames,Callback_t addedCallback,Callback_t removedCallback)2015dde864SJagpal Singh Gill EntityManagerInterface::EntityManagerInterface(
2115dde864SJagpal Singh Gill     sdbusplus::async::context& ctx, const interface_list_t& interfaceNames,
2215dde864SJagpal Singh Gill     Callback_t addedCallback, Callback_t removedCallback) :
2315dde864SJagpal Singh Gill     ctx(ctx), interfaceNames(interfaceNames),
2415dde864SJagpal Singh Gill     addedCallback(std::move(addedCallback)),
2515dde864SJagpal Singh Gill     removedCallback(std::move(removedCallback))
2615dde864SJagpal Singh Gill {
2715dde864SJagpal Singh Gill     ctx.spawn(handleInventoryAdded());
2815dde864SJagpal Singh Gill     ctx.spawn(handleInventoryRemoved());
2915dde864SJagpal Singh Gill }
3015dde864SJagpal Singh Gill 
handleInventoryGet()3115dde864SJagpal Singh Gill auto EntityManagerInterface::handleInventoryGet() -> sdbusplus::async::task<>
3215dde864SJagpal Singh Gill {
3315dde864SJagpal Singh Gill     if (!addedCallback)
3415dde864SJagpal Singh Gill     {
3515dde864SJagpal Singh Gill         error("addedCallback is not set");
3615dde864SJagpal Singh Gill         co_return;
3715dde864SJagpal Singh Gill     }
3815dde864SJagpal Singh Gill 
3915dde864SJagpal Singh Gill     using InventoryIntf =
4015dde864SJagpal Singh Gill         sdbusplus::client::xyz::openbmc_project::inventory::Item<>;
4115dde864SJagpal Singh Gill 
4215dde864SJagpal Singh Gill     constexpr auto entityManager =
4315dde864SJagpal Singh Gill         sdbusplus::async::proxy()
4415dde864SJagpal Singh Gill             .service(serviceName)
4515dde864SJagpal Singh Gill             .path(InventoryIntf::namespace_path)
4615dde864SJagpal Singh Gill             .interface("org.freedesktop.DBus.ObjectManager");
4715dde864SJagpal Singh Gill 
4815dde864SJagpal Singh Gill     for (const auto& [objectPath, detectorConfig] :
4915dde864SJagpal Singh Gill          co_await entityManager.call<ManagedObjectType>(ctx,
5015dde864SJagpal Singh Gill                                                         "GetManagedObjects"))
5115dde864SJagpal Singh Gill     {
5215dde864SJagpal Singh Gill         for (const auto& interfaceName : interfaceNames)
5315dde864SJagpal Singh Gill         {
5415dde864SJagpal Singh Gill             if (detectorConfig.contains(interfaceName))
5515dde864SJagpal Singh Gill             {
5615dde864SJagpal Singh Gill                 addedCallback(objectPath, interfaceName);
5715dde864SJagpal Singh Gill             }
5815dde864SJagpal Singh Gill         }
5915dde864SJagpal Singh Gill     }
6015dde864SJagpal Singh Gill 
6115dde864SJagpal Singh Gill     co_return;
6215dde864SJagpal Singh Gill }
6315dde864SJagpal Singh Gill 
handleInventoryAdded()6415dde864SJagpal Singh Gill auto EntityManagerInterface::handleInventoryAdded() -> sdbusplus::async::task<>
6515dde864SJagpal Singh Gill {
6615dde864SJagpal Singh Gill     if (!addedCallback)
6715dde864SJagpal Singh Gill     {
6815dde864SJagpal Singh Gill         error("addedCallback is not set");
6915dde864SJagpal Singh Gill         co_return;
7015dde864SJagpal Singh Gill     }
7115dde864SJagpal Singh Gill 
7215dde864SJagpal Singh Gill     auto addedMatch = sdbusplus::async::match(
7315dde864SJagpal Singh Gill         ctx, rules_intf::interfacesAdded() + rules_intf::sender(serviceName));
7415dde864SJagpal Singh Gill 
7515dde864SJagpal Singh Gill     while (!ctx.stop_requested())
7615dde864SJagpal Singh Gill     {
77*0d2f96f1SGeorge Liu         auto result = co_await addedMatch
7815dde864SJagpal Singh Gill                           .next<sdbusplus::message::object_path, SensorData>();
79*0d2f96f1SGeorge Liu         auto& [objectPath, inventoryData] = result;
8015dde864SJagpal Singh Gill 
8115dde864SJagpal Singh Gill         for (const auto& interfaceName : interfaceNames)
8215dde864SJagpal Singh Gill         {
8315dde864SJagpal Singh Gill             if (inventoryData.contains(interfaceName))
8415dde864SJagpal Singh Gill             {
8515dde864SJagpal Singh Gill                 addedCallback(objectPath, interfaceName);
8615dde864SJagpal Singh Gill             }
8715dde864SJagpal Singh Gill         }
8815dde864SJagpal Singh Gill     }
8915dde864SJagpal Singh Gill 
9015dde864SJagpal Singh Gill     co_return;
9115dde864SJagpal Singh Gill }
9215dde864SJagpal Singh Gill 
handleInventoryRemoved()9315dde864SJagpal Singh Gill auto EntityManagerInterface::handleInventoryRemoved()
9415dde864SJagpal Singh Gill     -> sdbusplus::async::task<>
9515dde864SJagpal Singh Gill {
9615dde864SJagpal Singh Gill     if (!removedCallback)
9715dde864SJagpal Singh Gill     {
9815dde864SJagpal Singh Gill         error("removedCallback is not set");
9915dde864SJagpal Singh Gill         co_return;
10015dde864SJagpal Singh Gill     }
10115dde864SJagpal Singh Gill 
10215dde864SJagpal Singh Gill     auto removedMatch = sdbusplus::async::match(
10315dde864SJagpal Singh Gill         ctx, rules_intf::interfacesRemoved() + rules_intf::sender(serviceName));
10415dde864SJagpal Singh Gill 
10515dde864SJagpal Singh Gill     while (!ctx.stop_requested())
10615dde864SJagpal Singh Gill     {
107*0d2f96f1SGeorge Liu         auto result =
10815dde864SJagpal Singh Gill             co_await removedMatch
10915dde864SJagpal Singh Gill                 .next<sdbusplus::message::object_path, interface_list_t>();
110*0d2f96f1SGeorge Liu         auto& [objectPath, interfaces] = result;
11115dde864SJagpal Singh Gill 
11215dde864SJagpal Singh Gill         for (const auto& interfaceName : interfaceNames)
11315dde864SJagpal Singh Gill         {
11415dde864SJagpal Singh Gill             if (std::ranges::find(interfaces, interfaceName) !=
11515dde864SJagpal Singh Gill                 interfaces.end())
11615dde864SJagpal Singh Gill             {
11715dde864SJagpal Singh Gill                 removedCallback(objectPath, interfaceName);
11815dde864SJagpal Singh Gill             }
11915dde864SJagpal Singh Gill         }
12015dde864SJagpal Singh Gill     }
12115dde864SJagpal Singh Gill 
12215dde864SJagpal Singh Gill     co_return;
12315dde864SJagpal Singh Gill }
12415dde864SJagpal Singh Gill 
12515dde864SJagpal Singh Gill } // namespace entity_manager
126