Refactored code. Fixed crash at start-up.
authorOssama Othman <ossama.othman@intel.com>
Wed, 11 Dec 2013 07:09:41 +0000 (23:09 -0800)
committerOssama Othman <ossama.othman@intel.com>
Wed, 11 Dec 2013 07:09:41 +0000 (23:09 -0800)
Change-Id: I7946598edac4f4856bef3639530291cd89bf7938
Signed-off-by: Ossama Othman <ossama.othman@intel.com>
plugins/connman/connman_manager.cpp
plugins/connman/connman_manager.hpp

index f1a6bb7..dae076a 100644 (file)
@@ -138,17 +138,13 @@ ivi::settings::connman_manager::connman_manager(
    *       method.
    */
   // Subscribe to PropertyChanged signal for all technologies.
-  constexpr gint const timeout = 5000;  // milliseconds
   GError * error = nullptr;
 
   unique_ptr<GVariant> const dictionary(
-    g_dbus_proxy_call_sync(connman_.proxy(),
-                           "GetTechnologies",
-                           nullptr,  // No parameters
-                           G_DBUS_CALL_FLAGS_NONE,
-                           timeout,
-                           nullptr,  // Not cancellable
-                           &error));
+    call_method("GetTechnologies",
+                nullptr,  // No parameters,
+                error));
+
   unique_ptr<GError> safe_error(error);
 
   if (dictionary != nullptr) {
@@ -180,6 +176,7 @@ ivi::settings::connman_manager::connman_manager(
                              + agent_.object_path());
   }
 
+  error = nullptr;
   unique_ptr<GVariant> const ret(
     call_method("RegisterAgent",
                 g_variant_new("(o)",
@@ -332,44 +329,68 @@ void
 ivi::settings::connman_manager::get_services(
   response_callback & response) const
 {
-  call_method("GetServices", response);
+  constexpr char name[] = "GetServices";
+  GError * error = nullptr;
+
+  unique_ptr<GVariant> ret(call_method(name,
+                                       nullptr,  // No parameters
+                                       error));
+
+  unique_ptr<GError> safe_error(error);
+
+  handle_return_value(name, ret.get(), error, response);
 }
 
 void
 ivi::settings::connman_manager::get_technologies(
   response_callback & response) const
 {
-  call_method("GetTechnologies", response);
+  constexpr char name[] = "GetTechnologies";
+  GError * error = nullptr;
+
+  unique_ptr<GVariant> ret(call_method(name,
+                                       nullptr,  // No parameters
+                                       error));
+
+  unique_ptr<GError> safe_error(error);
+
+  handle_return_value(name, ret.get(), error, response);
 }
 
-void
+GVariant *
 ivi::settings::connman_manager::call_method(
   char const * name,
-  response_callback & response) const
+  GVariant * parameters,
+  GError *& error) const
 {
   constexpr gint const timeout = 5000;  // milliseconds
-  GError * error = nullptr;
 
-  unique_ptr<GVariant> ret(
+  return
     g_dbus_proxy_call_sync(connman_.proxy(),
                            name,
-                           nullptr,  // No parameters
+                           parameters,
                            G_DBUS_CALL_FLAGS_NONE,
                            timeout,
                            nullptr,  // Not cancellable
-                           &error));
-
-  unique_ptr<GError> safe_error(error);
+                           &error);
+}
 
+void
+ivi::settings::connman_manager::handle_return_value(
+  char const * name,
+  GVariant * ret,
+  GError * error,
+  response_callback & response) const
+{
   if (ret != nullptr) {
     response.send_response(
-      [&ret](JsonBuilder * builder)
+      [ret](JsonBuilder * builder)
       {
         /**
          * @todo Can @c json_gvariant_serialize() ever return a nullptr?
          */
         JsonNode * const value =
-          json_gvariant_serialize(ret.get());
+          json_gvariant_serialize(ret);
 
         json_builder_set_member_name(builder, "value");
         json_builder_add_value(builder, value);
index 386603b..5a27e8f 100644 (file)
@@ -139,17 +139,31 @@ namespace ivi
       void get_technologies(response_callback & response) const;
 
       /**
-       * Call the method @a name on the connman Manager object.  This
-       * method is meant to be used for @c GetServices and
-       * @c GetTechnologies requests.
+       * Call the method @a name on the connman Manager object.
+       *
+       * @param[in]    name       The connman Manager object method
+       *                          name.
+       * @param[in]    parameters Method parameters.
+       * @param[inout] error      Pointer to error object containing
+       *                          failure related information.  Caller
+       *                          must deallocate if it is not null.
        *
-       * @param[in]    name     The connman Manager object method
-       *                        name.
-       * @param[inout] response Callback used to inform the caller of
-       *                        the results.
+       * @return Variant containing result of underlying Connman
+       *         Manager D-Bus call.  The caller must release the
+       *         returned @c GVariant.
        */
-      void call_method(char const * name,
-                       response_callback & response) const;
+      GVariant * call_method(char const * name,
+                             GVariant * parameters,
+                             GError *& error) const;
+
+
+      /**
+       * Send response or error to client that initiated the request.
+       */  
+      void handle_return_value(char const * name,
+                               GVariant * ret,
+                               GError * error,
+                               response_callback & response) const;
 
       typedef void (*ivi_signal_callback)(GDBusConnection * connection,
                                           char const * sender_name,
@@ -167,22 +181,6 @@ namespace ivi
 
     private:
 
-      /**
-       * Call the method @a name on the connman Manager object.
-       *
-       * @param[in]    name       The connman Manager object method
-       *                          name.
-       * @param[in]    parameters Method parameters.
-       * @param[inout] error      Pointer to error object containing
-       *                          failure related information.  Caller
-       *                          must deallocate if it is not null.
-       */
-      GVariant * call_method(char const * name,
-                             GVariant * parameters,
-                             GError *& error) const;
-
-    private:
-
       /// The proxy used to access the connman Manager D-Bus API.
       connman connman_;