Refactored connman signal callback to generic D-Bus signal callback.
[profile/ivi/settings-daemon.git] / plugins / connman / connman_manager.cpp
index 0be40e8..a6449d7 100644 (file)
@@ -1,7 +1,7 @@
 /**
  * @file connman_manager.cpp
  *
- * @brief Connman_Manager-based settings plugin.
+ * @brief Connman Manager operations.
  *
  * @author Ossama Othman @<ossama.othman@@intel.com@>
  *
 
 #include "connman_manager.hpp"
 
-#include <settingsd/json_glib_traits.hpp>
-#include <settingsd/reverse_lock.hpp>
+#include <settingsd/dbus_signal_callback.hpp>
+#include <settingsd/glib_traits.hpp>
+#include <settingsd/smart_ptr.hpp>
 
 #include <cstring>
 
 
-namespace
-{
-  void
-  on_services_changed(GDBusConnection * /* connection */,
-                      char const * /* sender_name */,
-                      char const * /* object_path */,
-                      char const * /* interface_name */,
-                      char const * /* signal_name */,
-                      GVariant   * parameters,
-                      gpointer     user_data)
-  {
-    gsize const num_params = g_variant_n_children(parameters);
-    if (num_params != 2) {
-      // We should never get here!
-      g_printerr("Number of ServicesChanged signal parameters "
-                 "is not 2: %" G_GSIZE_FORMAT "\n",
-                 num_params);
-
-      return;
-    }
-
-    using namespace ivi::settings;
-
-    // Changed services are found in the first ServicesChanged
-    // argument.
-    smart_ptr<GVariant> const changed_services(
-      g_variant_get_child_value(parameters, 0));
-
-    // Serialize the changed services into a JSON tree.  This will be
-    // an array of [object, dict], where "object" is the D-Bus object
-    // path, and "dict" is a dictionary of object-specific
-    // properties.
-    smart_ptr<JsonNode> services(
-      json_gvariant_serialize(changed_services.get()));
-
-    // Notify callers about the scan results.
-    typedef connman_manager::user_data user_data_type;
-
-    user_data_type * const p = static_cast<user_data_type *>(user_data);
-    connman_manager::promise_list_type & promises = p->promises;
-
-    // Synchronize access to the promises list.
-    std::lock_guard<std::mutex> lock(p->mutex);
-
-    // Note that the end() iterator must be retrieved during each loop
-    // iteration in case another thread caused another promise to be
-    // added to the list when the lock was temporarily unlocked during
-    // the promise::set_value() call below.
-    for (auto i = promises.begin(); i != promises.end(); ) {
-      auto & promise = *i;
-
-      {
-        // Release the mutex during the promise::set_value()
-        // call/notification so that we don't unnecessarily block
-        // threads attempting to get a services_changed promise.
-        typedef ivi::settings::reverse_lock<std::mutex> reverse_lock;
-        reverse_lock reverse(p->mutex);
-
-        std::lock_guard<reverse_lock> kcol(reverse);
-
-        // Thread that owns the corresponding future must transfer
-        // ownership of the JsonNode to something that will release
-        // the underlying JsonNode, e.g. a JsonBuilder via
-        // json_builder_add_value() or another smart_ptr<JsonNode>.
-        promise->set_value(std::move(services));
-      }
-
-      // Done with the pointer to the promise.  Remove it from the
-      // list now since we already have an iterator to it.
-      i = promises.erase(i);
-    }
-  }
-
-}
-
-// ---------------------------------------------------------------
-
-ivi::settings::connman_manager::connman_manager()
+ivi::settings::connman_manager::connman_manager(
+  event_callback const & e)
   : connman_("net.connman.Manager",     // Interface
-             "/")                       // Object path
-  , mutex_()
-  , promises_()
-  , data_(mutex_, promises_)
+             "/",                       // Object path
+             e)
+  , event_callback_(e)
   , subscription_id_(
       g_dbus_connection_signal_subscribe(
         g_dbus_proxy_get_connection(G_DBUS_PROXY(connman_.proxy())),
@@ -124,10 +48,18 @@ ivi::settings::connman_manager::connman_manager()
         connman_.object_path(),
         nullptr,
         G_DBUS_SIGNAL_FLAGS_NONE,
-        on_services_changed,
-        &data_,
+        on_dbus_signal,
+        &event_callback_,
         nullptr))
 {
+  // The ServicesChanged signal parameters are:
+  //
+  //   array{object, dict}, array{object})
+  //
+  // where "object" is the D-Bus object path, and "dict" is a
+  // dictionary of object-specific properties.  The first parameter is
+  // list of changed services.  The second is a list of removed
+  // services.
 }
 
 ivi::settings::connman_manager::~connman_manager()
@@ -201,25 +133,6 @@ ivi::settings::connman_manager::get_services(GError *& error) const
                                 &error);
 }
 
-ivi::settings::connman_manager::shared_promise_type
-ivi::settings::connman_manager::get_services_changed_promise()
-{
-  // This promise must exist long enough for the value to retrieved
-  // from the future.  Use a shared_ptr<> to make that possible.
-  shared_promise_type p = std::make_shared<promise_type>();
-
-  {
-    std::lock_guard<std::mutex> lock(mutex_);
-
-    // Add a new std::promise to the promises list.  The promise will
-    // only be used once, and will be removed once its value has been
-    // set.
-    promises_.push_back(p);
-  }
-
-  return p;
-}
-
 
 // Local Variables:
 // mode:c++