Ed Tanous | 911ac31 | 2017-08-15 09:37:42 -0700 | [diff] [blame] | 1 | #pragma once |
Ed Tanous | 911ac31 | 2017-08-15 09:37:42 -0700 | [diff] [blame] | 2 | #include <dbus_singleton.hpp> |
Ed Tanous | aa2e59c | 2018-04-12 12:17:20 -0700 | [diff] [blame] | 3 | #include <sdbusplus/bus/match.hpp> |
Ed Tanous | 911ac31 | 2017-08-15 09:37:42 -0700 | [diff] [blame] | 4 | #include <crow/app.h> |
| 5 | #include <boost/container/flat_map.hpp> |
| 6 | |
| 7 | namespace crow { |
| 8 | namespace dbus_monitor { |
| 9 | |
| 10 | struct DbusWebsocketSession { |
Ed Tanous | aa2e59c | 2018-04-12 12:17:20 -0700 | [diff] [blame] | 11 | std::vector<std::unique_ptr<sdbusplus::bus::match::match>> matches; |
Ed Tanous | 911ac31 | 2017-08-15 09:37:42 -0700 | [diff] [blame] | 12 | }; |
| 13 | |
Ed Tanous | 55c7b7a | 2018-05-22 15:27:24 -0700 | [diff] [blame^] | 14 | static boost::container::flat_map<crow::websocket::Connection*, |
Ed Tanous | 911ac31 | 2017-08-15 09:37:42 -0700 | [diff] [blame] | 15 | DbusWebsocketSession> |
| 16 | sessions; |
| 17 | |
Ed Tanous | 55c7b7a | 2018-05-22 15:27:24 -0700 | [diff] [blame^] | 18 | int onPropertyUpdate(sd_bus_message* m, void* userdata, |
| 19 | sd_bus_error* ret_error) { |
Ed Tanous | aa2e59c | 2018-04-12 12:17:20 -0700 | [diff] [blame] | 20 | if (ret_error == nullptr || sd_bus_error_is_set(ret_error)) { |
Ed Tanous | 55c7b7a | 2018-05-22 15:27:24 -0700 | [diff] [blame^] | 21 | BMCWEB_LOG_ERROR << "Sdbus error in on_property_update"; |
Ed Tanous | aa2e59c | 2018-04-12 12:17:20 -0700 | [diff] [blame] | 22 | return 0; |
Ed Tanous | 911ac31 | 2017-08-15 09:37:42 -0700 | [diff] [blame] | 23 | } |
Ed Tanous | aa2e59c | 2018-04-12 12:17:20 -0700 | [diff] [blame] | 24 | sdbusplus::message::message message(m); |
Ed Tanous | 55c7b7a | 2018-05-22 15:27:24 -0700 | [diff] [blame^] | 25 | std::string objectName; |
Ed Tanous | aa2e59c | 2018-04-12 12:17:20 -0700 | [diff] [blame] | 26 | std::vector< |
| 27 | std::pair<std::string, sdbusplus::message::variant< |
| 28 | std::string, bool, int64_t, uint64_t, double>>> |
| 29 | values; |
Ed Tanous | 55c7b7a | 2018-05-22 15:27:24 -0700 | [diff] [blame^] | 30 | message.read(objectName, values); |
Ed Tanous | aa2e59c | 2018-04-12 12:17:20 -0700 | [diff] [blame] | 31 | 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 Tanous | 55c7b7a | 2018-05-22 15:27:24 -0700 | [diff] [blame^] | 37 | std::string dataToSend = j.dump(); |
Ed Tanous | aa2e59c | 2018-04-12 12:17:20 -0700 | [diff] [blame] | 38 | |
Ed Tanous | 55c7b7a | 2018-05-22 15:27:24 -0700 | [diff] [blame^] | 39 | for (const std::pair<crow::websocket::Connection*, DbusWebsocketSession>& |
Ed Tanous | aa2e59c | 2018-04-12 12:17:20 -0700 | [diff] [blame] | 40 | session : sessions) { |
Ed Tanous | 55c7b7a | 2018-05-22 15:27:24 -0700 | [diff] [blame^] | 41 | session.first->sendText(dataToSend); |
Ed Tanous | aa2e59c | 2018-04-12 12:17:20 -0700 | [diff] [blame] | 42 | } |
Ed Tanous | 911ac31 | 2017-08-15 09:37:42 -0700 | [diff] [blame] | 43 | }; |
| 44 | |
| 45 | template <typename... Middlewares> |
Ed Tanous | 55c7b7a | 2018-05-22 15:27:24 -0700 | [diff] [blame^] | 46 | void requestRoutes(Crow<Middlewares...>& app) { |
| 47 | BMCWEB_ROUTE(app, "/dbus_monitor") |
Ed Tanous | 911ac31 | 2017-08-15 09:37:42 -0700 | [diff] [blame] | 48 | .websocket() |
Ed Tanous | 55c7b7a | 2018-05-22 15:27:24 -0700 | [diff] [blame^] | 49 | .onopen([&](crow::websocket::Connection& conn) { |
| 50 | std::string pathNamespace(conn.req.urlParams.get("path_namespace")); |
| 51 | if (pathNamespace.empty()) { |
| 52 | conn.sendText( |
Ed Tanous | 911ac31 | 2017-08-15 09:37:42 -0700 | [diff] [blame] | 53 | nlohmann::json({"error", "Did not specify path_namespace"})); |
| 54 | conn.close("error"); |
| 55 | } |
| 56 | sessions[&conn] = DbusWebsocketSession(); |
Ed Tanous | 55c7b7a | 2018-05-22 15:27:24 -0700 | [diff] [blame^] | 57 | std::string matchString( |
Ed Tanous | 911ac31 | 2017-08-15 09:37:42 -0700 | [diff] [blame] | 58 | "type='signal'," |
| 59 | "interface='org.freedesktop.DBus.Properties'," |
| 60 | "path_namespace='" + |
Ed Tanous | 55c7b7a | 2018-05-22 15:27:24 -0700 | [diff] [blame^] | 61 | pathNamespace + "'"); |
Ed Tanous | aa2e59c | 2018-04-12 12:17:20 -0700 | [diff] [blame] | 62 | sessions[&conn].matches.emplace_back( |
| 63 | std::make_unique<sdbusplus::bus::match::match>( |
Ed Tanous | 55c7b7a | 2018-05-22 15:27:24 -0700 | [diff] [blame^] | 64 | *crow::connections::systemBus, matchString, onPropertyUpdate)); |
Ed Tanous | 911ac31 | 2017-08-15 09:37:42 -0700 | [diff] [blame] | 65 | |
| 66 | }) |
Ed Tanous | 55c7b7a | 2018-05-22 15:27:24 -0700 | [diff] [blame^] | 67 | .onclose([&](crow::websocket::Connection& conn, |
Ed Tanous | 911ac31 | 2017-08-15 09:37:42 -0700 | [diff] [blame] | 68 | const std::string& reason) { sessions.erase(&conn); }) |
Ed Tanous | 55c7b7a | 2018-05-22 15:27:24 -0700 | [diff] [blame^] | 69 | .onmessage([&](crow::websocket::Connection& conn, const std::string& data, |
Ed Tanous | 911ac31 | 2017-08-15 09:37:42 -0700 | [diff] [blame] | 70 | bool is_binary) { |
Ed Tanous | 55c7b7a | 2018-05-22 15:27:24 -0700 | [diff] [blame^] | 71 | BMCWEB_LOG_ERROR << "Got unexpected message from client on sensorws"; |
Ed Tanous | 911ac31 | 2017-08-15 09:37:42 -0700 | [diff] [blame] | 72 | }); |
| 73 | } |
Ed Tanous | aa2e59c | 2018-04-12 12:17:20 -0700 | [diff] [blame] | 74 | } // namespace dbus_monitor |
Ed Tanous | 911ac31 | 2017-08-15 09:37:42 -0700 | [diff] [blame] | 75 | } // namespace crow |