From 2e4ccff6767996899616f295314f95290d416d09 Mon Sep 17 00:00:00 2001 From: "Breuninger Matthias (ETAS-DAP/XPC-Fe3)" Date: Fri, 31 Jan 2025 13:09:27 +0100 Subject: [PATCH] Add UnregisterInterfaceCallback --- src/lib/lldpctl.hpp | 34 ++++++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) diff --git a/src/lib/lldpctl.hpp b/src/lib/lldpctl.hpp index c0cf3dc2..add03ca8 100644 --- a/src/lib/lldpctl.hpp +++ b/src/lib/lldpctl.hpp @@ -494,6 +494,8 @@ template class LldpWatch { /** * @brief Register an interface specific callback on remote changes. * + * @note Only up to one callback can be registered per interface. + * * @param if_name The local interface to monitor. * @param callback Callback to trigger on remote changes. * @param ctx Optional context passed to @p callback. @@ -514,6 +516,12 @@ template class LldpWatch { std::scoped_lock lock { mutex_ }; + if (interface_callbacks_.contains(if_name) ) { + throw std::system_error(std::error_code(LLDPCTL_ERR_CANNOT_CREATE, + LldpErrCategory()), + "Callback already registered for interface '" + if_name + "'"); + } + /** * Note: * There's a race one way or the other - we decided to accept the one @@ -532,6 +540,32 @@ template class LldpWatch { std::make_pair(callback, const_cast(ctx))); } + /** + * @brief Unregister a previously registered interface specific callback + * on remote changes. + * + * @param if_name The local interface not to monitor anymore. + */ + void UnregisterInterfaceCallback(const std::string &if_name) + { + const auto interface { + LldpCtl().GetInterface(if_name) + }; + if (!interface.has_value()) { + throw std::system_error(std::error_code(LLDPCTL_ERR_NOT_EXIST, + LldpErrCategory()), + "Couldn't find interface '" + if_name + "'"); + } + + std::scoped_lock lock { mutex_ }; + + if (0 == interface_callbacks_.erase(if_name) ) { + throw std::system_error(std::error_code(LLDPCTL_ERR_NOT_EXIST, + LldpErrCategory()), + "No callback registered for interface '" + if_name + "'"); + } + } + private: static void WatchCallback(lldpctl_change_t change, lldpctl_atom_t *interface, lldpctl_atom_t *neighbor, void *p) -- 2.39.5