blob: 0543c7b9f54987e0f66405d0261dcade0c012efe [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
Ed Tanous911ac312017-08-15 09:37:42 -07005#include <boost/container/flat_map.hpp>
Ed Tanous9b243a42018-08-03 14:33:10 -07006#include <boost/container/flat_set.hpp>
Ed Tanous1abe55e2018-09-05 08:30:59 -07007#include <dbus_singleton.hpp>
Matt Spinler715748a2019-01-30 13:22:28 -06008#include <openbmc_dbus_rest.hpp>
Ed Tanous1abe55e2018-09-05 08:30:59 -07009#include <sdbusplus/bus/match.hpp>
William A. Kennington III0a63b1c2018-10-18 13:37:19 -070010#include <sdbusplus/message/types.hpp>
Ed Tanousabf2add2019-01-22 16:40:12 -080011#include <variant>
Ed Tanous9b243a42018-08-03 14:33:10 -070012
Ed Tanous1abe55e2018-09-05 08:30:59 -070013namespace nlohmann
14{
Ed Tanousabf2add2019-01-22 16:40:12 -080015template <typename... Args> struct adl_serializer<std::variant<Args...>>
Ed Tanous1abe55e2018-09-05 08:30:59 -070016{
Ed Tanousabf2add2019-01-22 16:40:12 -080017 static void to_json(json& j, const std::variant<Args...>& v)
Ed Tanous1abe55e2018-09-05 08:30:59 -070018 {
Ed Tanousabf2add2019-01-22 16:40:12 -080019 std::visit([&](auto&& val) { j = val; }, v);
Ed Tanous1abe55e2018-09-05 08:30:59 -070020 }
Ed Tanous9b243a42018-08-03 14:33:10 -070021};
Ed Tanous1abe55e2018-09-05 08:30:59 -070022} // namespace nlohmann
Ed Tanous911ac312017-08-15 09:37:42 -070023
Ed Tanous1abe55e2018-09-05 08:30:59 -070024namespace crow
25{
26namespace dbus_monitor
27{
Ed Tanous911ac312017-08-15 09:37:42 -070028
Ed Tanous1abe55e2018-09-05 08:30:59 -070029struct DbusWebsocketSession
30{
31 std::vector<std::unique_ptr<sdbusplus::bus::match::match>> matches;
32 boost::container::flat_set<std::string> interfaces;
Ed Tanous911ac312017-08-15 09:37:42 -070033};
34
Ed Tanous55c7b7a2018-05-22 15:27:24 -070035static boost::container::flat_map<crow::websocket::Connection*,
Ed Tanous911ac312017-08-15 09:37:42 -070036 DbusWebsocketSession>
37 sessions;
38
Ed Tanous9b243a42018-08-03 14:33:10 -070039inline int onPropertyUpdate(sd_bus_message* m, void* userdata,
Ed Tanous1abe55e2018-09-05 08:30:59 -070040 sd_bus_error* ret_error)
41{
42 if (ret_error == nullptr || sd_bus_error_is_set(ret_error))
43 {
44 BMCWEB_LOG_ERROR << "Got sdbus error on match";
45 return 0;
Ed Tanous9b243a42018-08-03 14:33:10 -070046 }
Ed Tanous1abe55e2018-09-05 08:30:59 -070047 crow::websocket::Connection* connection =
48 static_cast<crow::websocket::Connection*>(userdata);
49 auto thisSession = sessions.find(connection);
50 if (thisSession == sessions.end())
51 {
52 BMCWEB_LOG_ERROR << "Couldn't find dbus connection " << connection;
53 return 0;
54 }
55 sdbusplus::message::message message(m);
Ed Tanous1abe55e2018-09-05 08:30:59 -070056 nlohmann::json j{{"event", message.get_member()},
57 {"path", message.get_path()}};
58 if (strcmp(message.get_member(), "PropertiesChanged") == 0)
59 {
Matt Spinler715748a2019-01-30 13:22:28 -060060 nlohmann::json data;
61 int r = openbmc_mapper::convertDBusToJSON("sa{sv}as", message, data);
62 if (r < 0)
63 {
64 BMCWEB_LOG_ERROR << "convertDBusToJSON failed with " << r;
65 return 0;
66 }
67 if (!data.is_array())
68 {
69 BMCWEB_LOG_ERROR << "No data in PropertiesChanged signal";
70 return 0;
71 }
72
73 // data is type sa{sv}as and is an array[3] of string, object, array
74 j["interface"] = data[0];
75 j["properties"] = data[1];
Ed Tanous1abe55e2018-09-05 08:30:59 -070076 }
77 else if (strcmp(message.get_member(), "InterfacesAdded") == 0)
78 {
Matt Spinler715748a2019-01-30 13:22:28 -060079 nlohmann::json data;
80 int r = openbmc_mapper::convertDBusToJSON("oa{sa{sv}}", message, data);
81 if (r < 0)
Ed Tanous1abe55e2018-09-05 08:30:59 -070082 {
Matt Spinler715748a2019-01-30 13:22:28 -060083 BMCWEB_LOG_ERROR << "convertDBusToJSON failed with " << r;
84 return 0;
85 }
86
87 if (!data.is_array())
88 {
89 BMCWEB_LOG_ERROR << "No data in InterfacesAdded signal";
90 return 0;
91 }
92
93 // data is type oa{sa{sv}} which is an array[2] of string, object
94 for (auto& entry : data[1].items())
95 {
96 auto it = thisSession->second.interfaces.find(entry.key());
Ed Tanous1abe55e2018-09-05 08:30:59 -070097 if (it != thisSession->second.interfaces.end())
98 {
Matt Spinler715748a2019-01-30 13:22:28 -060099 j["interfaces"][entry.key()] = entry.value();
Ed Tanous1abe55e2018-09-05 08:30:59 -0700100 }
101 }
102 }
103 else
104 {
105 BMCWEB_LOG_CRITICAL << "message " << message.get_member()
106 << " was unexpected";
107 return 0;
108 }
Ed Tanous9b243a42018-08-03 14:33:10 -0700109
Ed Tanous1abe55e2018-09-05 08:30:59 -0700110 connection->sendText(j.dump());
111 return 0;
Ed Tanous271584a2019-07-09 16:24:22 -0700112}
Ed Tanous911ac312017-08-15 09:37:42 -0700113
Ed Tanous1abe55e2018-09-05 08:30:59 -0700114template <typename... Middlewares> void requestRoutes(Crow<Middlewares...>& app)
115{
116 BMCWEB_ROUTE(app, "/subscribe")
Ed Tanous8251ffe2019-10-10 14:33:54 -0700117 .requires({"Login"})
Ed Tanous1abe55e2018-09-05 08:30:59 -0700118 .websocket()
119 .onopen([&](crow::websocket::Connection& conn) {
120 BMCWEB_LOG_DEBUG << "Connection " << &conn << " opened";
121 sessions[&conn] = DbusWebsocketSession();
122 })
123 .onclose([&](crow::websocket::Connection& conn,
124 const std::string& reason) { sessions.erase(&conn); })
125 .onmessage([&](crow::websocket::Connection& conn,
126 const std::string& data, bool is_binary) {
127 DbusWebsocketSession& thisSession = sessions[&conn];
128 BMCWEB_LOG_DEBUG << "Connection " << &conn << " recevied " << data;
129 nlohmann::json j = nlohmann::json::parse(data, nullptr, false);
130 if (j.is_discarded())
131 {
132 BMCWEB_LOG_ERROR << "Unable to parse json data for monitor";
133 conn.close("Unable to parse json request");
Ed Tanous9b243a42018-08-03 14:33:10 -0700134 return;
Ed Tanous9b243a42018-08-03 14:33:10 -0700135 }
Ed Tanous1abe55e2018-09-05 08:30:59 -0700136 nlohmann::json::iterator interfaces = j.find("interfaces");
137 if (interfaces != j.end())
138 {
139 thisSession.interfaces.reserve(interfaces->size());
140 for (auto& interface : *interfaces)
141 {
142 const std::string* str =
143 interface.get_ptr<const std::string*>();
144 if (str != nullptr)
145 {
146 thisSession.interfaces.insert(*str);
147 }
148 }
149 }
150
151 nlohmann::json::iterator paths = j.find("paths");
152 if (paths != j.end())
153 {
Ed Tanous271584a2019-07-09 16:24:22 -0700154 size_t interfaceCount = thisSession.interfaces.size();
Ed Tanous1abe55e2018-09-05 08:30:59 -0700155 if (interfaceCount == 0)
156 {
157 interfaceCount = 1;
158 }
159 // Reserve our matches upfront. For each path there is 1 for
160 // interfacesAdded, and InterfaceCount number for
161 // PropertiesChanged
162 thisSession.matches.reserve(thisSession.matches.size() +
163 paths->size() *
Ed Tanous271584a2019-07-09 16:24:22 -0700164 (1U + interfaceCount));
Ed Tanous1abe55e2018-09-05 08:30:59 -0700165 }
166 std::string object_manager_match_string;
167 std::string properties_match_string;
168 std::string object_manager_interfaces_match_string;
169 // These regexes derived on the rules here:
170 // https://dbus.freedesktop.org/doc/dbus-specification.html#message-protocol-names
171 std::regex validPath("^/([A-Za-z0-9_]+/?)*$");
172 std::regex validInterface(
173 "^[A-Za-z_][A-Za-z0-9_]*(\\.[A-Za-z_][A-Za-z0-9_]*)+$");
174
175 for (const auto& thisPath : *paths)
176 {
177 const std::string* thisPathString =
178 thisPath.get_ptr<const std::string*>();
179 if (thisPathString == nullptr)
180 {
181 BMCWEB_LOG_ERROR << "subscribe path isn't a string?";
182 conn.close();
183 return;
184 }
185 if (!std::regex_match(*thisPathString, validPath))
186 {
187 BMCWEB_LOG_ERROR << "Invalid path name " << *thisPathString;
188 conn.close();
189 return;
190 }
191 properties_match_string =
192 ("type='signal',"
193 "interface='org.freedesktop.DBus.Properties',"
194 "path_namespace='" +
195 *thisPathString +
196 "',"
197 "member='PropertiesChanged'");
198 // If interfaces weren't specified, add a single match for all
199 // interfaces
200 if (thisSession.interfaces.size() == 0)
201 {
202 BMCWEB_LOG_DEBUG << "Creating match "
203 << properties_match_string;
204
205 thisSession.matches.emplace_back(
206 std::make_unique<sdbusplus::bus::match::match>(
207 *crow::connections::systemBus,
208 properties_match_string, onPropertyUpdate, &conn));
209 }
210 else
211 {
212 // If interfaces were specified, add a match for each
213 // interface
214 for (const std::string& interface : thisSession.interfaces)
215 {
216 if (!std::regex_match(interface, validInterface))
217 {
218 BMCWEB_LOG_ERROR << "Invalid interface name "
219 << interface;
220 conn.close();
221 return;
222 }
223 std::string ifaceMatchString = properties_match_string +
224 ",arg0='" + interface +
225 "'";
226 BMCWEB_LOG_DEBUG << "Creating match "
227 << ifaceMatchString;
228 thisSession.matches.emplace_back(
229 std::make_unique<sdbusplus::bus::match::match>(
230 *crow::connections::systemBus, ifaceMatchString,
231 onPropertyUpdate, &conn));
232 }
233 }
234 object_manager_match_string =
235 ("type='signal',"
236 "interface='org.freedesktop.DBus.ObjectManager',"
237 "path_namespace='" +
238 *thisPathString +
239 "',"
240 "member='InterfacesAdded'");
241 BMCWEB_LOG_DEBUG << "Creating match "
242 << object_manager_match_string;
243 thisSession.matches.emplace_back(
244 std::make_unique<sdbusplus::bus::match::match>(
245 *crow::connections::systemBus,
246 object_manager_match_string, onPropertyUpdate, &conn));
247 }
248 });
Ed Tanous911ac312017-08-15 09:37:42 -0700249}
Ed Tanous1abe55e2018-09-05 08:30:59 -0700250} // namespace dbus_monitor
251} // namespace crow