blob: ef50494a2c1e16b9d283c9863b4854355369b0ff [file] [log] [blame]
Brad Bishop4b916f12017-05-23 18:06:38 -04001#pragma once
2
Brad Bishopfccdc392017-05-22 21:11:09 -04003#include "callback.hpp"
Brad Bishop4b916f12017-05-23 18:06:38 -04004#include "data_types.hpp"
5#include "propertywatch.hpp"
6
Patrick Venture3d6d3182018-08-31 09:33:09 -07007#include <sdbusplus/bus/match.hpp>
8#include <sdbusplus/message.hpp>
Andrew Geisslerae4c95c2020-05-16 13:58:53 -05009#include <string>
Patrick Venture3d6d3182018-08-31 09:33:09 -070010#include <vector>
11
Brad Bishop4b916f12017-05-23 18:06:38 -040012namespace phosphor
13{
14namespace dbus
15{
16namespace monitoring
17{
18
Brad Bishop4b916f12017-05-23 18:06:38 -040019using MappedPropertyIndex =
20 RefKeyMap<const std::string,
Brad Bishopd1eac882018-03-29 10:34:05 -040021 RefKeyMap<const std::string, RefVector<const std::string>>>;
Brad Bishop4b916f12017-05-23 18:06:38 -040022
23MappedPropertyIndex convert(const PropertyIndex& index);
24
25template <typename DBusInterfaceType>
26void PropertyWatch<DBusInterfaceType>::start()
27{
28 if (alreadyRan)
29 {
30 return;
31 }
Brad Bishop4b916f12017-05-23 18:06:38 -040032
33 // The index has a flat layout which is not optimal here. Nest
34 // properties in a map of interface names in a map of object paths.
35 auto mapped = convert(index);
36
37 for (const auto& m : mapped)
38 {
39 const auto& path = m.first.get();
40 const auto& interfaces = m.second;
41
42 // Watch for new interfaces on this path.
43 DBusInterfaceType::addMatch(
44 sdbusplus::bus::match::rules::interfacesAdded(path),
Brad Bishopd1eac882018-03-29 10:34:05 -040045 [this](auto& msg)
46 // *INDENT-OFF*
47 { this->interfacesAdded(msg); });
Brad Bishop4b916f12017-05-23 18:06:38 -040048 // *INDENT-ON*
49
50 // Do a query to populate the cache. Start with a mapper query.
51 // The specific services are queried below.
Brad Bishopcca7a7c2018-07-09 22:34:33 -040052 auto getObjectFromMapper = [](const auto& path) {
53 const std::vector<std::string> queryInterfaces; // all interfaces
54 try
55 {
56 return DBusInterfaceType::template callMethodAndRead<GetObject>(
57 MAPPER_BUSNAME, MAPPER_PATH, MAPPER_INTERFACE, "GetObject",
58 path, queryInterfaces);
59 }
Patrick Williams764adb52021-09-02 09:36:47 -050060 catch (const sdbusplus::exception::exception&)
Brad Bishopcca7a7c2018-07-09 22:34:33 -040061 {
62 // Paths in the configuration may not exist yet. Prime those
63 // later, when/if InterfacesAdded occurs.
64 return GetObject();
65 }
66 };
67 auto mapperResp = getObjectFromMapper(path);
Brad Bishop4b916f12017-05-23 18:06:38 -040068
69 for (const auto& i : interfaces)
70 {
71 const auto& interface = i.first.get();
72
73 // Watch for property changes on this interface.
74 DBusInterfaceType::addMatch(
Brad Bishopd1eac882018-03-29 10:34:05 -040075 sdbusplus::bus::match::rules::propertiesChanged(path,
76 interface),
77 [this](auto& msg)
Brad Bishop4b916f12017-05-23 18:06:38 -040078 // *INDENT-OFF*
79 {
80 std::string interface;
81 msg.read(interface);
82 auto path = msg.get_path();
83 this->propertiesChanged(msg, path, interface);
84 });
Brad Bishopd1eac882018-03-29 10:34:05 -040085 // *INDENT-ON*
Brad Bishop4b916f12017-05-23 18:06:38 -040086
87 // The mapper response is a busname:[interfaces] map. Look for
88 // each interface in the index and if found, query the service and
89 // populate the cache entries for the interface.
90 for (const auto& mr : mapperResp)
91 {
92 const auto& busName = mr.first;
93 const auto& mapperInterfaces = mr.second;
Brad Bishopd1eac882018-03-29 10:34:05 -040094 if (mapperInterfaces.end() ==
95 std::find(mapperInterfaces.begin(), mapperInterfaces.end(),
96 interface))
Brad Bishop4b916f12017-05-23 18:06:38 -040097 {
98 // This interface isn't being watched.
99 continue;
100 }
101
102 // Delegate type specific property updates to subclasses.
Brad Bishopcca7a7c2018-07-09 22:34:33 -0400103 try
104 {
105 updateProperties(busName, path, interface);
106 }
Patrick Williams764adb52021-09-02 09:36:47 -0500107 catch (const sdbusplus::exception::exception&)
Brad Bishopcca7a7c2018-07-09 22:34:33 -0400108 {
109 // If for some reason the path has gone away since
110 // the mapper lookup we'll simply try again if/when
111 // InterfacesAdded occurs the next time it shows up.
112 }
Brad Bishop4b916f12017-05-23 18:06:38 -0400113 }
114 }
115 }
Brad Bishopc1283ae2017-05-20 21:42:38 -0400116
117 alreadyRan = true;
Brad Bishop4b916f12017-05-23 18:06:38 -0400118}
119
Brad Bishopce4fbe12017-06-06 23:58:09 -0400120template <typename DBusInterfaceType>
Ratan Guptaa45e0862018-02-21 19:03:13 +0530121void PropertyWatch<DBusInterfaceType>::callback(Context ctx)
Brad Bishopce4fbe12017-06-06 23:58:09 -0400122{
123 // Invoke callback if present.
124 if (this->alreadyRan && this->cb)
125 {
Ratan Guptaa45e0862018-02-21 19:03:13 +0530126 (*this->cb)(ctx);
Brad Bishopce4fbe12017-06-06 23:58:09 -0400127 }
128}
129
Brad Bishop4b916f12017-05-23 18:06:38 -0400130template <typename T, typename DBusInterfaceType>
131void PropertyWatchOfType<T, DBusInterfaceType>::updateProperties(
Brad Bishopd1eac882018-03-29 10:34:05 -0400132 const std::string& busName, const std::string& path,
Brad Bishop4b916f12017-05-23 18:06:38 -0400133 const std::string& interface)
134{
135 auto properties =
136 DBusInterfaceType::template callMethodAndRead<PropertiesChanged<T>>(
Brad Bishopd1eac882018-03-29 10:34:05 -0400137 busName.c_str(), path.c_str(), "org.freedesktop.DBus.Properties",
138 "GetAll", interface);
Brad Bishop4b916f12017-05-23 18:06:38 -0400139 propertiesChanged(path, interface, properties);
140}
141
142template <typename T, typename DBusInterfaceType>
143void PropertyWatchOfType<T, DBusInterfaceType>::propertiesChanged(
Brad Bishopd1eac882018-03-29 10:34:05 -0400144 const std::string& path, const std::string& interface,
Brad Bishop4b916f12017-05-23 18:06:38 -0400145 const PropertiesChanged<T>& properties)
146{
147 // Update the cache for any watched properties.
148 for (const auto& p : properties)
149 {
150 auto key = std::make_tuple(path, interface, p.first);
151 auto item = this->index.find(key);
152 if (item == this->index.end())
153 {
154 // This property isn't being watched.
155 continue;
156 }
157
Matthew Barthae786ef2019-09-04 15:46:13 -0500158 // Run property value thru filter operations
159 auto isFiltered = false;
160 const auto& storage = std::get<storageIndex>(item->second);
Patrick Williams34ef1e52020-05-13 11:07:43 -0500161 auto value = std::get<T>(p.second);
Matthew Barthefe01582019-09-09 15:22:37 -0500162 if (filterOps)
Matthew Barthae786ef2019-09-04 15:46:13 -0500163 {
Matthew Barthefe01582019-09-09 15:22:37 -0500164 any_ns::any anyValue = value;
165 if ((*filterOps)(anyValue))
Matthew Barthae786ef2019-09-04 15:46:13 -0500166 {
167 // Property value filtered, clear it from storage so
168 // callback functions do not use it
169 isFiltered = true;
170 std::get<valueIndex>(storage.get()).clear();
Matthew Barthae786ef2019-09-04 15:46:13 -0500171 }
172 }
173 if (!isFiltered)
174 {
175 // Property value not filtered out, update
176 std::get<valueIndex>(storage.get()) = value;
177 // Invoke callback if present.
178 this->callback(Context::SIGNAL);
179 }
Brad Bishop4b916f12017-05-23 18:06:38 -0400180 }
181}
182
183template <typename T, typename DBusInterfaceType>
184void PropertyWatchOfType<T, DBusInterfaceType>::propertiesChanged(
Brad Bishopd1eac882018-03-29 10:34:05 -0400185 sdbusplus::message::message& msg, const std::string& path,
Brad Bishop4b916f12017-05-23 18:06:38 -0400186 const std::string& interface)
187{
188 PropertiesChanged<T> properties;
189 msg.read(properties);
190 propertiesChanged(path, interface, properties);
191}
192
193template <typename T, typename DBusInterfaceType>
194void PropertyWatchOfType<T, DBusInterfaceType>::interfacesAdded(
Brad Bishopd1eac882018-03-29 10:34:05 -0400195 const std::string& path, const InterfacesAdded<T>& interfaces)
Brad Bishop4b916f12017-05-23 18:06:38 -0400196{
197 for (const auto& i : interfaces)
198 {
199 propertiesChanged(path, i.first, i.second);
200 }
201}
202
203template <typename T, typename DBusInterfaceType>
204void PropertyWatchOfType<T, DBusInterfaceType>::interfacesAdded(
205 sdbusplus::message::message& msg)
206{
207 sdbusplus::message::object_path path;
208 InterfacesAdded<T> interfaces;
209 msg.read(path, interfaces);
210 interfacesAdded(path, interfaces);
211}
212
213} // namespace monitoring
214} // namespace dbus
215} // namespace phosphor