diff options
| author | Yuvakumar Selvamani <yuvakumars@ami.com> | 2026-08-20 10:17:47 +0300 |
|---|---|---|
| committer | Yuvakumar Selvamani <yuvakumars@ami.com> | 2026-09-04 10:33:51 +0300 |
| commit | e10c52ed88e8ef349777f054733bcf9f5fc70ba3 (patch) | |
| tree | 22e815e17e2eaf806f310d4e6c8aa799d1c7d9cb | |
| parent | ebc3bff983cb94ede6ca80f0664cd7108ea8ec59 (diff) | |
| download | bmcweb-master.tar.xz | |
Extract the long onmessage() websocket lambda in requestRoutes() into a
named function, handleDbusMonitorMessage(), per the <10 line lambda
coding standard in docs/COMMON_ERRORS.md.
Tested:
- No other functional change.
- Tested on an AST2600 SoC platform.
- Redfish Service Validator passed with no new errors or warnings
introduced.
Change-Id: I340df11dfbc626e26dab931f0ca0581cbd17e102
Signed-off-by: Yuvakumar Selvamani <yuvakumars@ami.com>
| -rw-r--r-- | features/openbmc_rest/dbus_monitor.hpp | 256 |
1 files changed, 126 insertions, 130 deletions
diff --git a/features/openbmc_rest/dbus_monitor.hpp b/features/openbmc_rest/dbus_monitor.hpp index 4a04e657e0..31050934b3 100644 --- a/features/openbmc_rest/dbus_monitor.hpp +++ b/features/openbmc_rest/dbus_monitor.hpp @@ -131,152 +131,148 @@ inline int onPropertyUpdate(sd_bus_message* m, void* userdata, return 0; } -inline void requestRoutes(App& app) +inline void handleDbusMonitorMessage(crow::websocket::Connection& conn, + const std::string& data, bool /*unused*/) { - BMCWEB_ROUTE(app, "/subscribe/") - .privileges({{"Login"}}) - .websocket() - .onopen([](crow::websocket::Connection& conn) { - BMCWEB_LOG_DEBUG("Connection {} opened", logPtr(&conn)); - sessions.try_emplace(&conn); - }) - .onclose([](crow::websocket::Connection& conn, const std::string&) { - sessions.erase(&conn); - }) - // ast-grep-ignore: long-lambda - .onmessage([](crow::websocket::Connection& conn, - const std::string& data, bool) { - const auto sessionPair = sessions.find(&conn); - if (sessionPair == sessions.end()) - { - conn.close("Internal error"); - } - DbusWebsocketSession& thisSession = sessionPair->second; - BMCWEB_LOG_DEBUG("Connection {} received {}", logPtr(&conn), data); - std::optional<nlohmann::json> j = parseStringAsJson(data); - if (!j) - { - BMCWEB_LOG_ERROR("Unable to parse json data for monitor"); - conn.close("Unable to parse json request"); - return; - } - nlohmann::json::object_t* obj = - j->get_ptr<nlohmann::json::object_t*>(); - if (obj == nullptr) - { - BMCWEB_LOG_ERROR("Unable to parse json data for monitor"); - conn.close("Unable to parse json request"); - return; - } - nlohmann::json::object_t::iterator interfaces = - obj->find("interfaces"); - if (interfaces != obj->end()) + const auto sessionPair = sessions.find(&conn); + if (sessionPair == sessions.end()) + { + conn.close("Internal error"); + } + DbusWebsocketSession& thisSession = sessionPair->second; + BMCWEB_LOG_DEBUG("Connection {} received {}", logPtr(&conn), data); + std::optional<nlohmann::json> j = parseStringAsJson(data); + if (!j) + { + BMCWEB_LOG_ERROR("Unable to parse json data for monitor"); + conn.close("Unable to parse json request"); + return; + } + nlohmann::json::object_t* obj = j->get_ptr<nlohmann::json::object_t*>(); + if (obj == nullptr) + { + BMCWEB_LOG_ERROR("Unable to parse json data for monitor"); + conn.close("Unable to parse json request"); + return; + } + nlohmann::json::object_t::iterator interfaces = obj->find("interfaces"); + if (interfaces != obj->end()) + { + thisSession.interfaces.reserve(interfaces->second.size()); + for (const auto& interface : interfaces->second) + { + const std::string* str = interface.get_ptr<const std::string*>(); + if (str != nullptr) { - thisSession.interfaces.reserve(interfaces->second.size()); - for (const auto& interface : interfaces->second) - { - const std::string* str = - interface.get_ptr<const std::string*>(); - if (str != nullptr) - { - thisSession.interfaces.insert(*str); - } - } + thisSession.interfaces.insert(*str); } + } + } - nlohmann::json::object_t::iterator paths = obj->find("paths"); - if (paths == obj->end()) - { - BMCWEB_LOG_ERROR("Unable to find paths in json data"); - conn.close("Unable to find paths in json data"); - return; - } + nlohmann::json::object_t::iterator paths = obj->find("paths"); + if (paths == obj->end()) + { + BMCWEB_LOG_ERROR("Unable to find paths in json data"); + conn.close("Unable to find paths in json data"); + return; + } - size_t interfaceCount = thisSession.interfaces.size(); - if (interfaceCount == 0) - { - interfaceCount = 1; - } + size_t interfaceCount = thisSession.interfaces.size(); + if (interfaceCount == 0) + { + // NOLINTNEXTLINE(clang-analyzer-deadcode.DeadStores) + interfaceCount = 1; + } - // These regexes derived on the rules here: - // https://dbus.freedesktop.org/doc/dbus-specification.html#message-protocol-names - static std::regex validPath("^/([A-Za-z0-9_]+/?)*$"); - static std::regex validInterface( - "^[A-Za-z_][A-Za-z0-9_]*(\\.[A-Za-z_][A-Za-z0-9_]*)+$"); + // These regexes derived on the rules here: + // https://dbus.freedesktop.org/doc/dbus-specification.html#message-protocol-names + static std::regex validPath("^/([A-Za-z0-9_]+/?)*$"); + static std::regex validInterface( + "^[A-Za-z_][A-Za-z0-9_]*(\\.[A-Za-z_][A-Za-z0-9_]*)+$"); - for (const auto& thisPath : paths->second) + for (const auto& thisPath : paths->second) + { + const std::string* thisPathString = + thisPath.get_ptr<const std::string*>(); + if (thisPathString == nullptr) + { + BMCWEB_LOG_ERROR("subscribe path isn't a string?"); + conn.close(); + return; + } + if (!std::regex_match(*thisPathString, validPath)) + { + BMCWEB_LOG_ERROR("Invalid path name {}", *thisPathString); + conn.close(); + return; + } + std::string propertiesMatchString = + ("type='signal'," + "interface='org.freedesktop.DBus.Properties'," + "path_namespace='" + + *thisPathString + + "'," + "member='PropertiesChanged'"); + // If interfaces weren't specified, add a single match for all + // interfaces + if (thisSession.interfaces.empty()) + { + BMCWEB_LOG_DEBUG("Creating match {}", propertiesMatchString); + + thisSession.matches.emplace_back(std::make_unique<sdbusplus::match>( + *crow::connections::systemBus, propertiesMatchString, + onPropertyUpdate, &conn)); + } + else + { + // If interfaces were specified, add a match for each + // interface + for (const std::string& interface : thisSession.interfaces) { - const std::string* thisPathString = - thisPath.get_ptr<const std::string*>(); - if (thisPathString == nullptr) - { - BMCWEB_LOG_ERROR("subscribe path isn't a string?"); - conn.close(); - return; - } - if (!std::regex_match(*thisPathString, validPath)) + if (!std::regex_match(interface, validInterface)) { - BMCWEB_LOG_ERROR("Invalid path name {}", *thisPathString); + BMCWEB_LOG_ERROR("Invalid interface name {}", interface); conn.close(); return; } - std::string propertiesMatchString = - ("type='signal'," - "interface='org.freedesktop.DBus.Properties'," - "path_namespace='" + - *thisPathString + - "'," - "member='PropertiesChanged'"); - // If interfaces weren't specified, add a single match for all - // interfaces - if (thisSession.interfaces.empty()) - { - BMCWEB_LOG_DEBUG("Creating match {}", - propertiesMatchString); - - thisSession.matches.emplace_back( - std::make_unique<sdbusplus::match>( - *crow::connections::systemBus, - propertiesMatchString, onPropertyUpdate, &conn)); - } - else - { - // If interfaces were specified, add a match for each - // interface - for (const std::string& interface : thisSession.interfaces) - { - if (!std::regex_match(interface, validInterface)) - { - BMCWEB_LOG_ERROR("Invalid interface name {}", - interface); - conn.close(); - return; - } - std::string ifaceMatchString = propertiesMatchString; - ifaceMatchString += ",arg0='"; - ifaceMatchString += interface; - ifaceMatchString += "'"; - BMCWEB_LOG_DEBUG("Creating match {}", ifaceMatchString); - thisSession.matches.emplace_back( - std::make_unique<sdbusplus::match>( - *crow::connections::systemBus, ifaceMatchString, - onPropertyUpdate, &conn)); - } - } - std::string objectManagerMatchString = - ("type='signal'," - "interface='org.freedesktop.DBus.ObjectManager'," - "path_namespace='" + - *thisPathString + - "'," - "member='InterfacesAdded'"); - BMCWEB_LOG_DEBUG("Creating match {}", objectManagerMatchString); + std::string ifaceMatchString = propertiesMatchString; + ifaceMatchString += ",arg0='"; + ifaceMatchString += interface; + ifaceMatchString += "'"; + BMCWEB_LOG_DEBUG("Creating match {}", ifaceMatchString); thisSession.matches.emplace_back( std::make_unique<sdbusplus::match>( - *crow::connections::systemBus, objectManagerMatchString, + *crow::connections::systemBus, ifaceMatchString, onPropertyUpdate, &conn)); } - }); + } + std::string objectManagerMatchString = + ("type='signal'," + "interface='org.freedesktop.DBus.ObjectManager'," + "path_namespace='" + + *thisPathString + + "'," + "member='InterfacesAdded'"); + BMCWEB_LOG_DEBUG("Creating match {}", objectManagerMatchString); + thisSession.matches.emplace_back(std::make_unique<sdbusplus::match>( + *crow::connections::systemBus, objectManagerMatchString, + onPropertyUpdate, &conn)); + } +} + +inline void requestRoutes(App& app) +{ + BMCWEB_ROUTE(app, "/subscribe/") + .privileges({{"Login"}}) + .websocket() + .onopen([](crow::websocket::Connection& conn) { + BMCWEB_LOG_DEBUG("Connection {} opened", logPtr(&conn)); + sessions.try_emplace(&conn); + }) + .onclose([](crow::websocket::Connection& conn, const std::string&) { + sessions.erase(&conn); + }) + .onmessage(handleDbusMonitorMessage); } } // namespace dbus_monitor } // namespace crow |
