blob: 4a5c7c60ea99b92b65677bdfcc08040924471d65 [file] [log] [blame]
Ed Tanous911ac312017-08-15 09:37:42 -07001#pragma once
Ed Tanousc94ad492019-10-10 15:39:33 -07002#include <app.h>
3#include <websocket.h>
Ed Tanous1abe55e2018-09-05 08:30:59 -07004
Iwona Klimaszewskac0a1c8a2019-07-12 18:26:38 +02005#include <async_resp.hpp>
Ed Tanous911ac312017-08-15 09:37:42 -07006#include <boost/container/flat_map.hpp>
Ed Tanous9b243a42018-08-03 14:33:10 -07007#include <boost/container/flat_set.hpp>
Ed Tanous1abe55e2018-09-05 08:30:59 -07008#include <dbus_singleton.hpp>
Matt Spinler715748a2019-01-30 13:22:28 -06009#include <openbmc_dbus_rest.hpp>
Ed Tanous1abe55e2018-09-05 08:30:59 -070010#include <sdbusplus/bus/match.hpp>
William A. Kennington III0a63b1c2018-10-18 13:37:19 -070011#include <sdbusplus/message/types.hpp>
Gunnar Mills1214b7e2020-06-04 10:11:30 -050012
Ed Tanousabf2add2019-01-22 16:40:12 -080013#include <variant>
Ed Tanous9b243a42018-08-03 14:33:10 -070014
Ed Tanous1abe55e2018-09-05 08:30:59 -070015namespace nlohmann
16{
Gunnar Mills1214b7e2020-06-04 10:11:30 -050017template <typename... Args>
18struct adl_serializer<std::variant<Args...>>
Ed Tanous1abe55e2018-09-05 08:30:59 -070019{
Ed Tanousabf2add2019-01-22 16:40:12 -080020 static void to_json(json& j, const std::variant<Args...>& v)
Ed Tanous1abe55e2018-09-05 08:30:59 -070021 {
Ed Tanousabf2add2019-01-22 16:40:12 -080022 std::visit([&](auto&& val) { j = val; }, v);
Ed Tanous1abe55e2018-09-05 08:30:59 -070023 }
Ed Tanous9b243a42018-08-03 14:33:10 -070024};
Ed Tanous1abe55e2018-09-05 08:30:59 -070025} // namespace nlohmann
Ed Tanous911ac312017-08-15 09:37:42 -070026
Ed Tanous1abe55e2018-09-05 08:30:59 -070027namespace crow
28{
29namespace dbus_monitor
30{
Ed Tanous911ac312017-08-15 09:37:42 -070031
Ed Tanous1abe55e2018-09-05 08:30:59 -070032struct DbusWebsocketSession
33{
34 std::vector<std::unique_ptr<sdbusplus::bus::match::match>> matches;
35 boost::container::flat_set<std::string> interfaces;
Ed Tanous911ac312017-08-15 09:37:42 -070036};
37
Ed Tanous55c7b7a2018-05-22 15:27:24 -070038static boost::container::flat_map<crow::websocket::Connection*,
Ed Tanous911ac312017-08-15 09:37:42 -070039 DbusWebsocketSession>
40 sessions;
41
Ed Tanous9b243a42018-08-03 14:33:10 -070042inline int onPropertyUpdate(sd_bus_message* m, void* userdata,
Ed Tanous1abe55e2018-09-05 08:30:59 -070043 sd_bus_error* ret_error)
44{
45 if (ret_error == nullptr || sd_bus_error_is_set(ret_error))
46 {
47 BMCWEB_LOG_ERROR << "Got sdbus error on match";
48 return 0;
Ed Tanous9b243a42018-08-03 14:33:10 -070049 }
Ed Tanous1abe55e2018-09-05 08:30:59 -070050 crow::websocket::Connection* connection =
51 static_cast<crow::websocket::Connection*>(userdata);
52 auto thisSession = sessions.find(connection);
53 if (thisSession == sessions.end())
54 {
55 BMCWEB_LOG_ERROR << "Couldn't find dbus connection " << connection;
56 return 0;
57 }
58 sdbusplus::message::message message(m);
Ed Tanous1abe55e2018-09-05 08:30:59 -070059 nlohmann::json j{{"event", message.get_member()},
60 {"path", message.get_path()}};
61 if (strcmp(message.get_member(), "PropertiesChanged") == 0)
62 {
Matt Spinler715748a2019-01-30 13:22:28 -060063 nlohmann::json data;
64 int r = openbmc_mapper::convertDBusToJSON("sa{sv}as", message, data);
65 if (r < 0)
66 {
67 BMCWEB_LOG_ERROR << "convertDBusToJSON failed with " << r;
68 return 0;
69 }
70 if (!data.is_array())
71 {
72 BMCWEB_LOG_ERROR << "No data in PropertiesChanged signal";
73 return 0;
74 }
75
76 // data is type sa{sv}as and is an array[3] of string, object, array
77 j["interface"] = data[0];
78 j["properties"] = data[1];
Ed Tanous1abe55e2018-09-05 08:30:59 -070079 }
80 else if (strcmp(message.get_member(), "InterfacesAdded") == 0)
81 {
Matt Spinler715748a2019-01-30 13:22:28 -060082 nlohmann::json data;
83 int r = openbmc_mapper::convertDBusToJSON("oa{sa{sv}}", message, data);
84 if (r < 0)
Ed Tanous1abe55e2018-09-05 08:30:59 -070085 {
Matt Spinler715748a2019-01-30 13:22:28 -060086 BMCWEB_LOG_ERROR << "convertDBusToJSON failed with " << r;
87 return 0;
88 }
89
90 if (!data.is_array())
91 {
92 BMCWEB_LOG_ERROR << "No data in InterfacesAdded signal";
93 return 0;
94 }
95
96 // data is type oa{sa{sv}} which is an array[2] of string, object
97 for (auto& entry : data[1].items())
98 {
99 auto it = thisSession->second.interfaces.find(entry.key());
Ed Tanous1abe55e2018-09-05 08:30:59 -0700100 if (it != thisSession->second.interfaces.end())
101 {
Matt Spinler715748a2019-01-30 13:22:28 -0600102 j["interfaces"][entry.key()] = entry.value();
Ed Tanous1abe55e2018-09-05 08:30:59 -0700103 }
104 }
105 }
106 else
107 {
108 BMCWEB_LOG_CRITICAL << "message " << message.get_member()
109 << " was unexpected";
110 return 0;
111 }
Ed Tanous9b243a42018-08-03 14:33:10 -0700112
Ed Tanous1abe55e2018-09-05 08:30:59 -0700113 connection->sendText(j.dump());
114 return 0;
Ed Tanous271584a2019-07-09 16:24:22 -0700115}
Ed Tanous911ac312017-08-15 09:37:42 -0700116
Ed Tanous23a21a12020-07-25 04:45:05 +0000117inline void requestRoutes(App& app)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700118{
119 BMCWEB_ROUTE(app, "/subscribe")
Ed Tanous23a21a12020-07-25 04:45:05 +0000120 .privileges({"Login"})
Ed Tanous1abe55e2018-09-05 08:30:59 -0700121 .websocket()
Iwona Klimaszewskac0a1c8a2019-07-12 18:26:38 +0200122 .onopen([&](crow::websocket::Connection& conn,
123 std::shared_ptr<bmcweb::AsyncResp> asyncResp) {
Ed Tanous1abe55e2018-09-05 08:30:59 -0700124 BMCWEB_LOG_DEBUG << "Connection " << &conn << " opened";
125 sessions[&conn] = DbusWebsocketSession();
126 })
127 .onclose([&](crow::websocket::Connection& conn,
128 const std::string& reason) { sessions.erase(&conn); })
129 .onmessage([&](crow::websocket::Connection& conn,
130 const std::string& data, bool is_binary) {
131 DbusWebsocketSession& thisSession = sessions[&conn];
Gunnar Millscaa3ce32020-07-08 14:46:53 -0500132 BMCWEB_LOG_DEBUG << "Connection " << &conn << " received " << data;
Ed Tanous1abe55e2018-09-05 08:30:59 -0700133 nlohmann::json j = nlohmann::json::parse(data, nullptr, false);
134 if (j.is_discarded())
135 {
136 BMCWEB_LOG_ERROR << "Unable to parse json data for monitor";
137 conn.close("Unable to parse json request");
Ed Tanous9b243a42018-08-03 14:33:10 -0700138 return;
Ed Tanous9b243a42018-08-03 14:33:10 -0700139 }
Ed Tanous1abe55e2018-09-05 08:30:59 -0700140 nlohmann::json::iterator interfaces = j.find("interfaces");
141 if (interfaces != j.end())
142 {
143 thisSession.interfaces.reserve(interfaces->size());
144 for (auto& interface : *interfaces)
145 {
146 const std::string* str =
147 interface.get_ptr<const std::string*>();
148 if (str != nullptr)
149 {
150 thisSession.interfaces.insert(*str);
151 }
152 }
153 }
154
155 nlohmann::json::iterator paths = j.find("paths");
156 if (paths != j.end())
157 {
Ed Tanous271584a2019-07-09 16:24:22 -0700158 size_t interfaceCount = thisSession.interfaces.size();
Ed Tanous1abe55e2018-09-05 08:30:59 -0700159 if (interfaceCount == 0)
160 {
161 interfaceCount = 1;
162 }
163 // Reserve our matches upfront. For each path there is 1 for
164 // interfacesAdded, and InterfaceCount number for
165 // PropertiesChanged
166 thisSession.matches.reserve(thisSession.matches.size() +
167 paths->size() *
Ed Tanous271584a2019-07-09 16:24:22 -0700168 (1U + interfaceCount));
Ed Tanous1abe55e2018-09-05 08:30:59 -0700169 }
170 std::string object_manager_match_string;
171 std::string properties_match_string;
172 std::string object_manager_interfaces_match_string;
173 // These regexes derived on the rules here:
174 // https://dbus.freedesktop.org/doc/dbus-specification.html#message-protocol-names
175 std::regex validPath("^/([A-Za-z0-9_]+/?)*$");
176 std::regex validInterface(
177 "^[A-Za-z_][A-Za-z0-9_]*(\\.[A-Za-z_][A-Za-z0-9_]*)+$");
178
179 for (const auto& thisPath : *paths)
180 {
181 const std::string* thisPathString =
182 thisPath.get_ptr<const std::string*>();
183 if (thisPathString == nullptr)
184 {
185 BMCWEB_LOG_ERROR << "subscribe path isn't a string?";
186 conn.close();
187 return;
188 }
189 if (!std::regex_match(*thisPathString, validPath))
190 {
191 BMCWEB_LOG_ERROR << "Invalid path name " << *thisPathString;
192 conn.close();
193 return;
194 }
195 properties_match_string =
196 ("type='signal',"
197 "interface='org.freedesktop.DBus.Properties',"
198 "path_namespace='" +
199 *thisPathString +
200 "',"
201 "member='PropertiesChanged'");
202 // If interfaces weren't specified, add a single match for all
203 // interfaces
204 if (thisSession.interfaces.size() == 0)
205 {
206 BMCWEB_LOG_DEBUG << "Creating match "
207 << properties_match_string;
208
209 thisSession.matches.emplace_back(
210 std::make_unique<sdbusplus::bus::match::match>(
211 *crow::connections::systemBus,
212 properties_match_string, onPropertyUpdate, &conn));
213 }
214 else
215 {
216 // If interfaces were specified, add a match for each
217 // interface
218 for (const std::string& interface : thisSession.interfaces)
219 {
220 if (!std::regex_match(interface, validInterface))
221 {
222 BMCWEB_LOG_ERROR << "Invalid interface name "
223 << interface;
224 conn.close();
225 return;
226 }
227 std::string ifaceMatchString = properties_match_string +
228 ",arg0='" + interface +
229 "'";
230 BMCWEB_LOG_DEBUG << "Creating match "
231 << ifaceMatchString;
232 thisSession.matches.emplace_back(
233 std::make_unique<sdbusplus::bus::match::match>(
234 *crow::connections::systemBus, ifaceMatchString,
235 onPropertyUpdate, &conn));
236 }
237 }
238 object_manager_match_string =
239 ("type='signal',"
240 "interface='org.freedesktop.DBus.ObjectManager',"
241 "path_namespace='" +
242 *thisPathString +
243 "',"
244 "member='InterfacesAdded'");
245 BMCWEB_LOG_DEBUG << "Creating match "
246 << object_manager_match_string;
247 thisSession.matches.emplace_back(
248 std::make_unique<sdbusplus::bus::match::match>(
249 *crow::connections::systemBus,
250 object_manager_match_string, onPropertyUpdate, &conn));
251 }
252 });
Ed Tanous911ac312017-08-15 09:37:42 -0700253}
Ed Tanous1abe55e2018-09-05 08:30:59 -0700254} // namespace dbus_monitor
255} // namespace crow