blob: 1747810c686d5803c22043b4ad00de244edd7699 [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>
Ed Tanousabf2add2019-01-22 16:40:12 -080012#include <variant>
Ed Tanous9b243a42018-08-03 14:33:10 -070013
Ed Tanous1abe55e2018-09-05 08:30:59 -070014namespace nlohmann
15{
Ed Tanousabf2add2019-01-22 16:40:12 -080016template <typename... Args> struct adl_serializer<std::variant<Args...>>
Ed Tanous1abe55e2018-09-05 08:30:59 -070017{
Ed Tanousabf2add2019-01-22 16:40:12 -080018 static void to_json(json& j, const std::variant<Args...>& v)
Ed Tanous1abe55e2018-09-05 08:30:59 -070019 {
Ed Tanousabf2add2019-01-22 16:40:12 -080020 std::visit([&](auto&& val) { j = val; }, v);
Ed Tanous1abe55e2018-09-05 08:30:59 -070021 }
Ed Tanous9b243a42018-08-03 14:33:10 -070022};
Ed Tanous1abe55e2018-09-05 08:30:59 -070023} // namespace nlohmann
Ed Tanous911ac312017-08-15 09:37:42 -070024
Ed Tanous1abe55e2018-09-05 08:30:59 -070025namespace crow
26{
27namespace dbus_monitor
28{
Ed Tanous911ac312017-08-15 09:37:42 -070029
Ed Tanous1abe55e2018-09-05 08:30:59 -070030struct DbusWebsocketSession
31{
32 std::vector<std::unique_ptr<sdbusplus::bus::match::match>> matches;
33 boost::container::flat_set<std::string> interfaces;
Ed Tanous911ac312017-08-15 09:37:42 -070034};
35
Ed Tanous55c7b7a2018-05-22 15:27:24 -070036static boost::container::flat_map<crow::websocket::Connection*,
Ed Tanous911ac312017-08-15 09:37:42 -070037 DbusWebsocketSession>
38 sessions;
39
Ed Tanous9b243a42018-08-03 14:33:10 -070040inline int onPropertyUpdate(sd_bus_message* m, void* userdata,
Ed Tanous1abe55e2018-09-05 08:30:59 -070041 sd_bus_error* ret_error)
42{
43 if (ret_error == nullptr || sd_bus_error_is_set(ret_error))
44 {
45 BMCWEB_LOG_ERROR << "Got sdbus error on match";
46 return 0;
Ed Tanous9b243a42018-08-03 14:33:10 -070047 }
Ed Tanous1abe55e2018-09-05 08:30:59 -070048 crow::websocket::Connection* connection =
49 static_cast<crow::websocket::Connection*>(userdata);
50 auto thisSession = sessions.find(connection);
51 if (thisSession == sessions.end())
52 {
53 BMCWEB_LOG_ERROR << "Couldn't find dbus connection " << connection;
54 return 0;
55 }
56 sdbusplus::message::message message(m);
Ed Tanous1abe55e2018-09-05 08:30:59 -070057 nlohmann::json j{{"event", message.get_member()},
58 {"path", message.get_path()}};
59 if (strcmp(message.get_member(), "PropertiesChanged") == 0)
60 {
Matt Spinler715748a2019-01-30 13:22:28 -060061 nlohmann::json data;
62 int r = openbmc_mapper::convertDBusToJSON("sa{sv}as", message, data);
63 if (r < 0)
64 {
65 BMCWEB_LOG_ERROR << "convertDBusToJSON failed with " << r;
66 return 0;
67 }
68 if (!data.is_array())
69 {
70 BMCWEB_LOG_ERROR << "No data in PropertiesChanged signal";
71 return 0;
72 }
73
74 // data is type sa{sv}as and is an array[3] of string, object, array
75 j["interface"] = data[0];
76 j["properties"] = data[1];
Ed Tanous1abe55e2018-09-05 08:30:59 -070077 }
78 else if (strcmp(message.get_member(), "InterfacesAdded") == 0)
79 {
Matt Spinler715748a2019-01-30 13:22:28 -060080 nlohmann::json data;
81 int r = openbmc_mapper::convertDBusToJSON("oa{sa{sv}}", message, data);
82 if (r < 0)
Ed Tanous1abe55e2018-09-05 08:30:59 -070083 {
Matt Spinler715748a2019-01-30 13:22:28 -060084 BMCWEB_LOG_ERROR << "convertDBusToJSON failed with " << r;
85 return 0;
86 }
87
88 if (!data.is_array())
89 {
90 BMCWEB_LOG_ERROR << "No data in InterfacesAdded signal";
91 return 0;
92 }
93
94 // data is type oa{sa{sv}} which is an array[2] of string, object
95 for (auto& entry : data[1].items())
96 {
97 auto it = thisSession->second.interfaces.find(entry.key());
Ed Tanous1abe55e2018-09-05 08:30:59 -070098 if (it != thisSession->second.interfaces.end())
99 {
Matt Spinler715748a2019-01-30 13:22:28 -0600100 j["interfaces"][entry.key()] = entry.value();
Ed Tanous1abe55e2018-09-05 08:30:59 -0700101 }
102 }
103 }
104 else
105 {
106 BMCWEB_LOG_CRITICAL << "message " << message.get_member()
107 << " was unexpected";
108 return 0;
109 }
Ed Tanous9b243a42018-08-03 14:33:10 -0700110
Ed Tanous1abe55e2018-09-05 08:30:59 -0700111 connection->sendText(j.dump());
112 return 0;
Ed Tanous271584a2019-07-09 16:24:22 -0700113}
Ed Tanous911ac312017-08-15 09:37:42 -0700114
Ed Tanous1abe55e2018-09-05 08:30:59 -0700115template <typename... Middlewares> void requestRoutes(Crow<Middlewares...>& app)
116{
117 BMCWEB_ROUTE(app, "/subscribe")
Ed Tanous8251ffe2019-10-10 14:33:54 -0700118 .requires({"Login"})
Ed Tanous1abe55e2018-09-05 08:30:59 -0700119 .websocket()
Iwona Klimaszewskac0a1c8a2019-07-12 18:26:38 +0200120 .onopen([&](crow::websocket::Connection& conn,
121 std::shared_ptr<bmcweb::AsyncResp> asyncResp) {
Ed Tanous1abe55e2018-09-05 08:30:59 -0700122 BMCWEB_LOG_DEBUG << "Connection " << &conn << " opened";
123 sessions[&conn] = DbusWebsocketSession();
124 })
125 .onclose([&](crow::websocket::Connection& conn,
126 const std::string& reason) { sessions.erase(&conn); })
127 .onmessage([&](crow::websocket::Connection& conn,
128 const std::string& data, bool is_binary) {
129 DbusWebsocketSession& thisSession = sessions[&conn];
130 BMCWEB_LOG_DEBUG << "Connection " << &conn << " recevied " << data;
131 nlohmann::json j = nlohmann::json::parse(data, nullptr, false);
132 if (j.is_discarded())
133 {
134 BMCWEB_LOG_ERROR << "Unable to parse json data for monitor";
135 conn.close("Unable to parse json request");
Ed Tanous9b243a42018-08-03 14:33:10 -0700136 return;
Ed Tanous9b243a42018-08-03 14:33:10 -0700137 }
Ed Tanous1abe55e2018-09-05 08:30:59 -0700138 nlohmann::json::iterator interfaces = j.find("interfaces");
139 if (interfaces != j.end())
140 {
141 thisSession.interfaces.reserve(interfaces->size());
142 for (auto& interface : *interfaces)
143 {
144 const std::string* str =
145 interface.get_ptr<const std::string*>();
146 if (str != nullptr)
147 {
148 thisSession.interfaces.insert(*str);
149 }
150 }
151 }
152
153 nlohmann::json::iterator paths = j.find("paths");
154 if (paths != j.end())
155 {
Ed Tanous271584a2019-07-09 16:24:22 -0700156 size_t interfaceCount = thisSession.interfaces.size();
Ed Tanous1abe55e2018-09-05 08:30:59 -0700157 if (interfaceCount == 0)
158 {
159 interfaceCount = 1;
160 }
161 // Reserve our matches upfront. For each path there is 1 for
162 // interfacesAdded, and InterfaceCount number for
163 // PropertiesChanged
164 thisSession.matches.reserve(thisSession.matches.size() +
165 paths->size() *
Ed Tanous271584a2019-07-09 16:24:22 -0700166 (1U + interfaceCount));
Ed Tanous1abe55e2018-09-05 08:30:59 -0700167 }
168 std::string object_manager_match_string;
169 std::string properties_match_string;
170 std::string object_manager_interfaces_match_string;
171 // These regexes derived on the rules here:
172 // https://dbus.freedesktop.org/doc/dbus-specification.html#message-protocol-names
173 std::regex validPath("^/([A-Za-z0-9_]+/?)*$");
174 std::regex validInterface(
175 "^[A-Za-z_][A-Za-z0-9_]*(\\.[A-Za-z_][A-Za-z0-9_]*)+$");
176
177 for (const auto& thisPath : *paths)
178 {
179 const std::string* thisPathString =
180 thisPath.get_ptr<const std::string*>();
181 if (thisPathString == nullptr)
182 {
183 BMCWEB_LOG_ERROR << "subscribe path isn't a string?";
184 conn.close();
185 return;
186 }
187 if (!std::regex_match(*thisPathString, validPath))
188 {
189 BMCWEB_LOG_ERROR << "Invalid path name " << *thisPathString;
190 conn.close();
191 return;
192 }
193 properties_match_string =
194 ("type='signal',"
195 "interface='org.freedesktop.DBus.Properties',"
196 "path_namespace='" +
197 *thisPathString +
198 "',"
199 "member='PropertiesChanged'");
200 // If interfaces weren't specified, add a single match for all
201 // interfaces
202 if (thisSession.interfaces.size() == 0)
203 {
204 BMCWEB_LOG_DEBUG << "Creating match "
205 << properties_match_string;
206
207 thisSession.matches.emplace_back(
208 std::make_unique<sdbusplus::bus::match::match>(
209 *crow::connections::systemBus,
210 properties_match_string, onPropertyUpdate, &conn));
211 }
212 else
213 {
214 // If interfaces were specified, add a match for each
215 // interface
216 for (const std::string& interface : thisSession.interfaces)
217 {
218 if (!std::regex_match(interface, validInterface))
219 {
220 BMCWEB_LOG_ERROR << "Invalid interface name "
221 << interface;
222 conn.close();
223 return;
224 }
225 std::string ifaceMatchString = properties_match_string +
226 ",arg0='" + interface +
227 "'";
228 BMCWEB_LOG_DEBUG << "Creating match "
229 << ifaceMatchString;
230 thisSession.matches.emplace_back(
231 std::make_unique<sdbusplus::bus::match::match>(
232 *crow::connections::systemBus, ifaceMatchString,
233 onPropertyUpdate, &conn));
234 }
235 }
236 object_manager_match_string =
237 ("type='signal',"
238 "interface='org.freedesktop.DBus.ObjectManager',"
239 "path_namespace='" +
240 *thisPathString +
241 "',"
242 "member='InterfacesAdded'");
243 BMCWEB_LOG_DEBUG << "Creating match "
244 << object_manager_match_string;
245 thisSession.matches.emplace_back(
246 std::make_unique<sdbusplus::bus::match::match>(
247 *crow::connections::systemBus,
248 object_manager_match_string, onPropertyUpdate, &conn));
249 }
250 });
Ed Tanous911ac312017-08-15 09:37:42 -0700251}
Ed Tanous1abe55e2018-09-05 08:30:59 -0700252} // namespace dbus_monitor
253} // namespace crow