blob: 43385e657168db4c8a76813fae18a1e04842ece3 [file] [log] [blame]
Ed Tanous911ac312017-08-15 09:37:42 -07001#pragma once
Ed Tanous911ac312017-08-15 09:37:42 -07002#include <dbus_singleton.hpp>
Ed Tanousaa2e59c2018-04-12 12:17:20 -07003#include <sdbusplus/bus/match.hpp>
Ed Tanous911ac312017-08-15 09:37:42 -07004#include <crow/app.h>
5#include <boost/container/flat_map.hpp>
6
7namespace crow {
8namespace dbus_monitor {
9
10struct DbusWebsocketSession {
Ed Tanousaa2e59c2018-04-12 12:17:20 -070011 std::vector<std::unique_ptr<sdbusplus::bus::match::match>> matches;
Ed Tanous911ac312017-08-15 09:37:42 -070012};
13
Ed Tanous55c7b7a2018-05-22 15:27:24 -070014static boost::container::flat_map<crow::websocket::Connection*,
Ed Tanous911ac312017-08-15 09:37:42 -070015 DbusWebsocketSession>
16 sessions;
17
Ed Tanous55c7b7a2018-05-22 15:27:24 -070018int onPropertyUpdate(sd_bus_message* m, void* userdata,
19 sd_bus_error* ret_error) {
Ed Tanousaa2e59c2018-04-12 12:17:20 -070020 if (ret_error == nullptr || sd_bus_error_is_set(ret_error)) {
Ed Tanous55c7b7a2018-05-22 15:27:24 -070021 BMCWEB_LOG_ERROR << "Sdbus error in on_property_update";
Ed Tanousaa2e59c2018-04-12 12:17:20 -070022 return 0;
Ed Tanous911ac312017-08-15 09:37:42 -070023 }
Ed Tanousaa2e59c2018-04-12 12:17:20 -070024 sdbusplus::message::message message(m);
Ed Tanous55c7b7a2018-05-22 15:27:24 -070025 std::string objectName;
Ed Tanousaa2e59c2018-04-12 12:17:20 -070026 std::vector<
27 std::pair<std::string, sdbusplus::message::variant<
28 std::string, bool, int64_t, uint64_t, double>>>
29 values;
Ed Tanous55c7b7a2018-05-22 15:27:24 -070030 message.read(objectName, values);
Ed Tanousaa2e59c2018-04-12 12:17:20 -070031 nlohmann::json j;
32 const std::string& path = message.get_path();
33 for (auto& value : values) {
34 mapbox::util::apply_visitor([&](auto&& val) { j[path] = val; },
35 value.second);
36 }
Ed Tanous55c7b7a2018-05-22 15:27:24 -070037 std::string dataToSend = j.dump();
Ed Tanousaa2e59c2018-04-12 12:17:20 -070038
Ed Tanous55c7b7a2018-05-22 15:27:24 -070039 for (const std::pair<crow::websocket::Connection*, DbusWebsocketSession>&
Ed Tanousaa2e59c2018-04-12 12:17:20 -070040 session : sessions) {
Ed Tanous55c7b7a2018-05-22 15:27:24 -070041 session.first->sendText(dataToSend);
Ed Tanousaa2e59c2018-04-12 12:17:20 -070042 }
Ed Tanous911ac312017-08-15 09:37:42 -070043};
44
45template <typename... Middlewares>
Ed Tanous55c7b7a2018-05-22 15:27:24 -070046void requestRoutes(Crow<Middlewares...>& app) {
47 BMCWEB_ROUTE(app, "/dbus_monitor")
Ed Tanous911ac312017-08-15 09:37:42 -070048 .websocket()
Ed Tanous55c7b7a2018-05-22 15:27:24 -070049 .onopen([&](crow::websocket::Connection& conn) {
50 std::string pathNamespace(conn.req.urlParams.get("path_namespace"));
51 if (pathNamespace.empty()) {
52 conn.sendText(
Ed Tanous911ac312017-08-15 09:37:42 -070053 nlohmann::json({"error", "Did not specify path_namespace"}));
54 conn.close("error");
55 }
56 sessions[&conn] = DbusWebsocketSession();
Ed Tanous55c7b7a2018-05-22 15:27:24 -070057 std::string matchString(
Ed Tanous911ac312017-08-15 09:37:42 -070058 "type='signal',"
59 "interface='org.freedesktop.DBus.Properties',"
60 "path_namespace='" +
Ed Tanous55c7b7a2018-05-22 15:27:24 -070061 pathNamespace + "'");
Ed Tanousaa2e59c2018-04-12 12:17:20 -070062 sessions[&conn].matches.emplace_back(
63 std::make_unique<sdbusplus::bus::match::match>(
Ed Tanous55c7b7a2018-05-22 15:27:24 -070064 *crow::connections::systemBus, matchString, onPropertyUpdate));
Ed Tanous911ac312017-08-15 09:37:42 -070065
66 })
Ed Tanous55c7b7a2018-05-22 15:27:24 -070067 .onclose([&](crow::websocket::Connection& conn,
Ed Tanous911ac312017-08-15 09:37:42 -070068 const std::string& reason) { sessions.erase(&conn); })
Ed Tanous55c7b7a2018-05-22 15:27:24 -070069 .onmessage([&](crow::websocket::Connection& conn, const std::string& data,
Ed Tanous911ac312017-08-15 09:37:42 -070070 bool is_binary) {
Ed Tanous55c7b7a2018-05-22 15:27:24 -070071 BMCWEB_LOG_ERROR << "Got unexpected message from client on sensorws";
Ed Tanous911ac312017-08-15 09:37:42 -070072 });
73}
Ed Tanousaa2e59c2018-04-12 12:17:20 -070074} // namespace dbus_monitor
Ed Tanous911ac312017-08-15 09:37:42 -070075} // namespace crow