+static int subscribe_unsubscribe_signal(struct afb_req request, bool subscribe, const std::string& sig, DiagnosticRequest* diag_req, int frequency)
+{
+ int ret;
+ sd_event_source *source;
+
+ std::lock_guard<std::mutex> subscribed_signals_lock(get_subscribed_signals_mutex());
+ std::map<std::string, struct afb_event>& s = get_subscribed_signals();
+ if (s.find(sig) != s.end() && !afb_event_is_valid(s[sig]))
+ {
+ if(!subscribe)
+ {
+ NOTICE(binder_interface, "Event isn't valid, it can't be unsubscribed.");
+ ret = -1;
+ }
+ else
+ {
+ /* Event it isn't valid anymore, recreate it */
+ sd_event_add_time(afb_daemon_get_event_loop(binder_interface->daemon), &source, CLOCK_MONOTONIC, frequency, 0,
+ configuration_t::instance().get_diagnostic_manager().send_request, diag_req);
+ ret = create_event_handle(sig, s);
+ }
+ }
+ else
+ {
+ /* Event doesn't exist , so let's create it */
+ struct afb_event empty_event = {nullptr, nullptr};
+ subscribed_signals[sig] = empty_event;
+ ret = create_event_handle(sig, s);
+ }
+
+ /* Check whether or not the event handler has been correctly created and
+ * make the subscription/unsubscription operation is so.
+ */
+ if (ret <= 0)
+ return ret;
+ return make_subscription_unsubscription(request, sig, s, subscribe);
+}
+