13b025e69SAndrew Geissler #include "processing.hpp"
23b025e69SAndrew Geissler
31e94e60bSBrad Bishop #include <algorithm>
41e94e60bSBrad Bishop #include <array>
570461896SAndrew Geissler #include <iostream>
686d2880eSBrad Bishop #include <string>
71e94e60bSBrad Bishop #include <string_view>
83b025e69SAndrew Geissler
getWellKnown(const boost::container::flat_map<std::string,std::string> & owners,const std::string & request,std::string & wellKnown)93b025e69SAndrew Geissler bool getWellKnown(
103b025e69SAndrew Geissler const boost::container::flat_map<std::string, std::string>& owners,
113b025e69SAndrew Geissler const std::string& request, std::string& wellKnown)
123b025e69SAndrew Geissler {
133b025e69SAndrew Geissler // If it's already a well known name, just return
1486d2880eSBrad Bishop if (!request.starts_with(":"))
153b025e69SAndrew Geissler {
163b025e69SAndrew Geissler wellKnown = request;
173b025e69SAndrew Geissler return true;
183b025e69SAndrew Geissler }
193b025e69SAndrew Geissler
203b025e69SAndrew Geissler auto it = owners.find(request);
213b025e69SAndrew Geissler if (it == owners.end())
223b025e69SAndrew Geissler {
233b025e69SAndrew Geissler return false;
243b025e69SAndrew Geissler }
253b025e69SAndrew Geissler wellKnown = it->second;
263b025e69SAndrew Geissler return true;
273b025e69SAndrew Geissler }
2882815dacSAndrew Geissler
needToIntrospect(const std::string & processName)291e94e60bSBrad Bishop bool needToIntrospect(const std::string& processName)
3082815dacSAndrew Geissler {
311e94e60bSBrad Bishop using namespace std::string_view_literals;
321e94e60bSBrad Bishop static constexpr std::array<std::string_view, 2> skipNamespaces{
331e94e60bSBrad Bishop ":"sv, "org.freedesktop"sv};
3482815dacSAndrew Geissler
351e94e60bSBrad Bishop auto inSkipList = std::find_if(skipNamespaces.begin(), skipNamespaces.end(),
361e94e60bSBrad Bishop [&processName](auto prefix) {
371e94e60bSBrad Bishop return processName.starts_with(prefix);
381e94e60bSBrad Bishop }) != skipNamespaces.end();
391e94e60bSBrad Bishop return !(inSkipList || processName.empty());
4082815dacSAndrew Geissler }
412067926aSAndrew Geissler
processNameChangeDelete(boost::asio::io_context & io,boost::container::flat_map<std::string,std::string> & nameOwners,const std::string & wellKnown,const std::string & oldOwner,InterfaceMapType & interfaceMap,AssociationMaps & assocMaps,sdbusplus::asio::object_server & server)422067926aSAndrew Geissler void processNameChangeDelete(
435b4357daSKallas, Pawel boost::asio::io_context& io,
442067926aSAndrew Geissler boost::container::flat_map<std::string, std::string>& nameOwners,
452067926aSAndrew Geissler const std::string& wellKnown, const std::string& oldOwner,
46a098a37aSBrad Bishop InterfaceMapType& interfaceMap, AssociationMaps& assocMaps,
472067926aSAndrew Geissler sdbusplus::asio::object_server& server)
482067926aSAndrew Geissler {
4986d2880eSBrad Bishop if (oldOwner.starts_with(":"))
502067926aSAndrew Geissler {
512067926aSAndrew Geissler auto it = nameOwners.find(oldOwner);
522067926aSAndrew Geissler if (it != nameOwners.end())
532067926aSAndrew Geissler {
542067926aSAndrew Geissler nameOwners.erase(it);
552067926aSAndrew Geissler }
562067926aSAndrew Geissler }
572067926aSAndrew Geissler // Connection removed
58a098a37aSBrad Bishop InterfaceMapType::iterator pathIt = interfaceMap.begin();
592067926aSAndrew Geissler while (pathIt != interfaceMap.end())
602067926aSAndrew Geissler {
612067926aSAndrew Geissler // If an associations interface is being removed,
622067926aSAndrew Geissler // also need to remove the corresponding associations
632067926aSAndrew Geissler // objects and properties.
642067926aSAndrew Geissler auto ifaces = pathIt->second.find(wellKnown);
652067926aSAndrew Geissler if (ifaces != pathIt->second.end())
662067926aSAndrew Geissler {
67d0cf9428SJohn Wang auto assoc = std::find(ifaces->second.begin(), ifaces->second.end(),
68d0cf9428SJohn Wang assocDefsInterface);
692067926aSAndrew Geissler if (assoc != ifaces->second.end())
702067926aSAndrew Geissler {
715b4357daSKallas, Pawel removeAssociation(io, pathIt->first, wellKnown, server,
725b4357daSKallas, Pawel assocMaps);
732067926aSAndrew Geissler }
749c3d2859SMatt Spinler
759c3d2859SMatt Spinler // Instead of checking if every single path is the endpoint of an
769c3d2859SMatt Spinler // association that needs to be moved to pending, only check when
779c3d2859SMatt Spinler // we own this path as well, which would be because of an
789c3d2859SMatt Spinler // association.
799c3d2859SMatt Spinler if ((pathIt->second.size() == 2) &&
80a02cd54cSBrad Bishop (pathIt->second.find("xyz.openbmc_project.ObjectMapper") !=
81a02cd54cSBrad Bishop pathIt->second.end()))
829c3d2859SMatt Spinler {
839c3d2859SMatt Spinler // Remove the 2 association D-Bus paths and move the
849c3d2859SMatt Spinler // association to pending.
855b4357daSKallas, Pawel moveAssociationToPending(io, pathIt->first, assocMaps, server);
869c3d2859SMatt Spinler }
872067926aSAndrew Geissler }
882067926aSAndrew Geissler pathIt->second.erase(wellKnown);
892067926aSAndrew Geissler if (pathIt->second.empty())
902067926aSAndrew Geissler {
912067926aSAndrew Geissler // If the last connection to the object is gone,
922067926aSAndrew Geissler // delete the top level object
932067926aSAndrew Geissler pathIt = interfaceMap.erase(pathIt);
942067926aSAndrew Geissler continue;
952067926aSAndrew Geissler }
962067926aSAndrew Geissler pathIt++;
972067926aSAndrew Geissler }
982067926aSAndrew Geissler }
9970461896SAndrew Geissler
processInterfaceAdded(boost::asio::io_context & io,InterfaceMapType & interfaceMap,const sdbusplus::message::object_path & objPath,const InterfacesAdded & intfAdded,const std::string & wellKnown,AssociationMaps & assocMaps,sdbusplus::asio::object_server & server)1009052ebd3SPatrick Williams void processInterfaceAdded(
1019052ebd3SPatrick Williams boost::asio::io_context& io, InterfaceMapType& interfaceMap,
10270461896SAndrew Geissler const sdbusplus::message::object_path& objPath,
1039052ebd3SPatrick Williams const InterfacesAdded& intfAdded, const std::string& wellKnown,
1049052ebd3SPatrick Williams AssociationMaps& assocMaps, sdbusplus::asio::object_server& server)
10570461896SAndrew Geissler {
10670461896SAndrew Geissler auto& ifaceList = interfaceMap[objPath.str];
10770461896SAndrew Geissler
10870461896SAndrew Geissler for (const auto& interfacePair : intfAdded)
10970461896SAndrew Geissler {
11070461896SAndrew Geissler ifaceList[wellKnown].emplace(interfacePair.first);
11170461896SAndrew Geissler
112d0cf9428SJohn Wang if (interfacePair.first == assocDefsInterface)
11370461896SAndrew Geissler {
1142bb2d6baSPatrick Williams const std::variant<std::vector<Association>>* variantAssociations =
1152bb2d6baSPatrick Williams nullptr;
11670461896SAndrew Geissler for (const auto& interface : interfacePair.second)
11770461896SAndrew Geissler {
118d0cf9428SJohn Wang if (interface.first == assocDefsProperty)
11970461896SAndrew Geissler {
12070461896SAndrew Geissler variantAssociations = &(interface.second);
12170461896SAndrew Geissler }
12270461896SAndrew Geissler }
12370461896SAndrew Geissler if (variantAssociations == nullptr)
12470461896SAndrew Geissler {
12570461896SAndrew Geissler std::cerr << "Illegal association found on " << wellKnown
12670461896SAndrew Geissler << "\n";
12770461896SAndrew Geissler continue;
12870461896SAndrew Geissler }
12970461896SAndrew Geissler std::vector<Association> associations =
130b05bc12cSPatrick Williams std::get<std::vector<Association>>(*variantAssociations);
1315b4357daSKallas, Pawel associationChanged(io, server, associations, objPath.str, wellKnown,
132e0b0e3a2SMatt Spinler interfaceMap, assocMaps);
13370461896SAndrew Geissler }
13470461896SAndrew Geissler }
13570461896SAndrew Geissler
13670461896SAndrew Geissler // To handle the case where an object path is being created
13770461896SAndrew Geissler // with 2 or more new path segments, check if the parent paths
13870461896SAndrew Geissler // of this path are already in the interface map, and add them
13970461896SAndrew Geissler // if they aren't with just the default freedesktop interfaces.
14070461896SAndrew Geissler // This would be done via introspection if they would have
14170461896SAndrew Geissler // already existed at startup. While we could also introspect
14270461896SAndrew Geissler // them now to do the work, we know there aren't any other
14370461896SAndrew Geissler // interfaces or we would have gotten signals for them as well,
14470461896SAndrew Geissler // so take a shortcut to speed things up.
14570461896SAndrew Geissler //
14670461896SAndrew Geissler // This is all needed so that mapper operations can be done
14770461896SAndrew Geissler // on the new parent paths.
148a098a37aSBrad Bishop using iface_map_iterator = InterfaceMapType::iterator;
149ea0e5d27SEd Tanous using name_map_iterator = ConnectionNames::iterator;
15070461896SAndrew Geissler
15170461896SAndrew Geissler std::string parent = objPath.str;
15270461896SAndrew Geissler auto pos = parent.find_last_of('/');
15370461896SAndrew Geissler
15470461896SAndrew Geissler while (pos != std::string::npos)
15570461896SAndrew Geissler {
15670461896SAndrew Geissler parent = parent.substr(0, pos);
15770461896SAndrew Geissler
15870461896SAndrew Geissler std::pair<iface_map_iterator, bool> parentEntry =
159ea0e5d27SEd Tanous interfaceMap.emplace(parent, ConnectionNames{});
16070461896SAndrew Geissler
16170461896SAndrew Geissler std::pair<name_map_iterator, bool> ifaceEntry =
162*7cc8d00aSLei YU parentEntry.first->second.emplace(wellKnown, InterfaceNames{});
16370461896SAndrew Geissler
16470461896SAndrew Geissler if (!ifaceEntry.second)
16570461896SAndrew Geissler {
16670461896SAndrew Geissler // Entry was already there for this name so done.
16770461896SAndrew Geissler break;
16870461896SAndrew Geissler }
16970461896SAndrew Geissler
17070461896SAndrew Geissler pos = parent.find_last_of('/');
17170461896SAndrew Geissler }
17211401e2eSMatt Spinler
17311401e2eSMatt Spinler // The new interface might have an association pending
1745b4357daSKallas, Pawel checkIfPendingAssociation(io, objPath.str, interfaceMap, assocMaps, server);
17570461896SAndrew Geissler }
176