[PATCH 1/3] bearer-qmi: port _connect to use GTask

Aleksander Morgado aleksander at aleksander.es
Mon Jul 3 08:39:12 UTC 2017


On 01/07/17 09:16, Ben Chan wrote:
> ---
>  src/mm-bearer-qmi.c | 185 ++++++++++++++++++++++++++++------------------------
>  1 file changed, 100 insertions(+), 85 deletions(-)
> 

Pushed to git master, thanks.

> diff --git a/src/mm-bearer-qmi.c b/src/mm-bearer-qmi.c
> index ae83a1c9..0ce4b6be 100644
> --- a/src/mm-bearer-qmi.c
> +++ b/src/mm-bearer-qmi.c
> @@ -247,8 +247,6 @@ typedef enum {
>  
>  typedef struct {
>      MMBearerQmi *self;
> -    GSimpleAsyncResult *result;
> -    GCancellable *cancellable;
>      ConnectStep step;
>      MMPort *data;
>      MMPortQmi *qmi;
> @@ -281,10 +279,8 @@ typedef struct {
>  } ConnectContext;
>  
>  static void
> -connect_context_complete_and_free (ConnectContext *ctx)
> +connect_context_free (ConnectContext *ctx)
>  {
> -    g_simple_async_result_complete_in_idle (ctx->result);
> -    g_object_unref (ctx->result);
>      g_free (ctx->apn);
>      g_free (ctx->user);
>      g_free (ctx->password);
> @@ -320,7 +316,6 @@ connect_context_complete_and_free (ConnectContext *ctx)
>      g_clear_object (&ctx->ipv6_config);
>      g_object_unref (ctx->data);
>      g_object_unref (ctx->qmi);
> -    g_object_unref (ctx->cancellable);
>      g_object_unref (ctx->self);
>      g_slice_free (ConnectContext, ctx);
>  }
> @@ -330,22 +325,21 @@ connect_finish (MMBaseBearer *self,
>                  GAsyncResult *res,
>                  GError **error)
>  {
> -    if (g_simple_async_result_propagate_error (G_SIMPLE_ASYNC_RESULT (res), error))
> -        return NULL;
> -
> -    return mm_bearer_connect_result_ref (g_simple_async_result_get_op_res_gpointer (G_SIMPLE_ASYNC_RESULT (res)));
> +    return g_task_propagate_pointer (G_TASK (res), error);
>  }
>  
> -static void connect_context_step (ConnectContext *ctx);
> +static void connect_context_step (GTask *task);
>  
>  static void
>  start_network_ready (QmiClientWds *client,
>                       GAsyncResult *res,
> -                     ConnectContext *ctx)
> +                     GTask *task)
>  {
> +    ConnectContext *ctx;
>      GError *error = NULL;
>      QmiMessageWdsStartNetworkOutput *output;
>  
> +    ctx = g_task_get_task_data (task);
>      g_assert (ctx->running_ipv4 || ctx->running_ipv6);
>      g_assert (!(ctx->running_ipv4 && ctx->running_ipv6));
>  
> @@ -416,7 +410,7 @@ start_network_ready (QmiClientWds *client,
>  
>      /* Keep on */
>      ctx->step++;
> -    connect_context_step (ctx);
> +    connect_context_step (task);
>  }
>  
>  static QmiMessageWdsStartNetworkInput *
> @@ -660,11 +654,13 @@ get_ipv6_config (MMBearerQmi *self,
>  static void
>  get_current_settings_ready (QmiClientWds *client,
>                              GAsyncResult *res,
> -                            ConnectContext *ctx)
> +                            GTask *task)
>  {
> +    ConnectContext *ctx;
>      GError *error = NULL;
>      QmiMessageWdsGetCurrentSettingsOutput *output;
>  
> +    ctx = g_task_get_task_data (task);
>      g_assert (ctx->running_ipv4 || ctx->running_ipv6);
>  
>      output = qmi_client_wds_get_current_settings_finish (client, res, &error);
> @@ -721,15 +717,17 @@ get_current_settings_ready (QmiClientWds *client,
>  
>      /* Keep on */
>      ctx->step++;
> -    connect_context_step (ctx);
> +    connect_context_step (task);
>  }
>  
>  static void
> -get_current_settings (ConnectContext *ctx, QmiClientWds *client)
> +get_current_settings (GTask *task, QmiClientWds *client)
>  {
> +    ConnectContext *ctx;
>      QmiMessageWdsGetCurrentSettingsInput *input;
>      QmiWdsGetCurrentSettingsRequestedSettings requested;
>  
> +    ctx = g_task_get_task_data (task);
>      g_assert (ctx->running_ipv4 || ctx->running_ipv6);
>  
>      requested = QMI_WDS_GET_CURRENT_SETTINGS_REQUESTED_SETTINGS_DNS_ADDRESS |
> @@ -745,20 +743,22 @@ get_current_settings (ConnectContext *ctx, QmiClientWds *client)
>      qmi_client_wds_get_current_settings (client,
>                                           input,
>                                           10,
> -                                         ctx->cancellable,
> +                                         g_task_get_cancellable (task),
>                                           (GAsyncReadyCallback)get_current_settings_ready,
> -                                         ctx);
> +                                         task);
>      qmi_message_wds_get_current_settings_input_unref (input);
>  }
>  
>  static void
>  set_ip_family_ready (QmiClientWds *client,
>                       GAsyncResult *res,
> -                     ConnectContext *ctx)
> +                     GTask *task)
>  {
> +    ConnectContext *ctx;
>      GError *error = NULL;
>      QmiMessageWdsSetIpFamilyOutput *output;
>  
> +    ctx = g_task_get_task_data (task);
>      g_assert (ctx->running_ipv4 || ctx->running_ipv6);
>      g_assert (!(ctx->running_ipv4 && ctx->running_ipv6));
>  
> @@ -780,7 +780,7 @@ set_ip_family_ready (QmiClientWds *client,
>  
>      /* Keep on */
>      ctx->step++;
> -    connect_context_step (ctx);
> +    connect_context_step (task);
>  }
>  
>  static void
> @@ -885,8 +885,11 @@ connect_enable_indications_ready (QmiClientWds *client,
>  static void
>  connect_enable_indications_ipv4_ready (QmiClientWds *client,
>                                         GAsyncResult *res,
> -                                       ConnectContext *ctx)
> +                                       GTask *task)
>  {
> +    ConnectContext *ctx;
> +
> +    ctx = g_task_get_task_data (task);
>      g_assert (ctx->event_report_ipv4_indication_id == 0);
>  
>      ctx->event_report_ipv4_indication_id =
> @@ -897,14 +900,17 @@ connect_enable_indications_ipv4_ready (QmiClientWds *client,
>      }
>  
>      ctx->step++;
> -    connect_context_step (ctx);
> +    connect_context_step (task);
>  }
>  
>  static void
>  connect_enable_indications_ipv6_ready (QmiClientWds *client,
>                                         GAsyncResult *res,
> -                                       ConnectContext *ctx)
> +                                       GTask *task)
>  {
> +    ConnectContext *ctx;
> +
> +    ctx = g_task_get_task_data (task);
>      g_assert (ctx->event_report_ipv6_indication_id == 0);
>  
>      ctx->event_report_ipv6_indication_id =
> @@ -915,7 +921,7 @@ connect_enable_indications_ipv6_ready (QmiClientWds *client,
>      }
>  
>      ctx->step++;
> -    connect_context_step (ctx);
> +    connect_context_step (task);
>  }
>  
>  static QmiMessageWdsSetEventReportInput *
> @@ -982,16 +988,18 @@ cleanup_event_report_unsolicited_events (MMBearerQmi *self,
>  static void
>  qmi_port_allocate_client_ready (MMPortQmi *qmi,
>                                  GAsyncResult *res,
> -                                ConnectContext *ctx)
> +                                GTask *task)
>  {
> +    ConnectContext *ctx;
>      GError *error = NULL;
>  
> +    ctx = g_task_get_task_data (task);
>      g_assert (ctx->running_ipv4 || ctx->running_ipv6);
>      g_assert (!(ctx->running_ipv4 && ctx->running_ipv6));
>  
>      if (!mm_port_qmi_allocate_client_finish (qmi, res, &error)) {
> -        g_simple_async_result_take_error (ctx->result, error);
> -        connect_context_complete_and_free (ctx);
> +        g_task_return_error (task, error);
> +        g_object_unref (task);
>          return;
>      }
>  
> @@ -1006,40 +1014,44 @@ qmi_port_allocate_client_ready (MMPortQmi *qmi,
>  
>      /* Keep on */
>      ctx->step++;
> -    connect_context_step (ctx);
> +    connect_context_step (task);
>  }
>  
>  static void
>  qmi_port_open_ready (MMPortQmi *qmi,
>                       GAsyncResult *res,
> -                     ConnectContext *ctx)
> +                     GTask *task)
>  {
> +    ConnectContext *ctx;
>      GError *error = NULL;
>  
>      if (!mm_port_qmi_open_finish (qmi, res, &error)) {
> -        g_simple_async_result_take_error (ctx->result, error);
> -        connect_context_complete_and_free (ctx);
> +        g_task_return_error (task, error);
> +        g_object_unref (task);
>          return;
>      }
>  
>      /* Keep on */
> +    ctx = g_task_get_task_data (task);
>      ctx->step++;
> -    connect_context_step (ctx);
> +    connect_context_step (task);
>  }
>  
>  static void
> -connect_context_step (ConnectContext *ctx)
> +connect_context_step (GTask *task)
>  {
> +    ConnectContext *ctx;
> +    GCancellable *cancellable;
> +
>      /* If cancelled, complete */
> -    if (g_cancellable_is_cancelled (ctx->cancellable)) {
> -        g_simple_async_result_set_error (ctx->result,
> -                                         MM_CORE_ERROR,
> -                                         MM_CORE_ERROR_CANCELLED,
> -                                         "Connection setup operation has been cancelled");
> -        connect_context_complete_and_free (ctx);
> +    if (g_task_return_error_if_cancelled (task)) {
> +        g_object_unref (task);
>          return;
>      }
>  
> +    ctx = g_task_get_task_data (task);
> +    cancellable = g_task_get_cancellable (task);
> +
>      switch (ctx->step) {
>      case CONNECT_STEP_FIRST:
>  
> @@ -1052,9 +1064,9 @@ connect_context_step (ConnectContext *ctx)
>          if (!mm_port_qmi_is_open (ctx->qmi)) {
>              mm_port_qmi_open (ctx->qmi,
>                                TRUE,
> -                              ctx->cancellable,
> +                              cancellable,
>                                (GAsyncReadyCallback)qmi_port_open_ready,
> -                              ctx);
> +                              task);
>              return;
>          }
>  
> @@ -1080,7 +1092,7 @@ connect_context_step (ConnectContext *ctx)
>          /* If no IPv4 setup needed, jump to IPv6 */
>          if (!ctx->ipv4) {
>              ctx->step = CONNECT_STEP_IPV6;
> -            connect_context_step (ctx);
> +            connect_context_step (task);
>              return;
>          }
>  
> @@ -1102,9 +1114,9 @@ connect_context_step (ConnectContext *ctx)
>              mm_port_qmi_allocate_client (ctx->qmi,
>                                           QMI_SERVICE_WDS,
>                                           MM_PORT_QMI_FLAG_WDS_IPV4,
> -                                         ctx->cancellable,
> +                                         cancellable,
>                                           (GAsyncReadyCallback)qmi_port_allocate_client_ready,
> -                                         ctx);
> +                                         task);
>              return;
>          }
>  
> @@ -1125,9 +1137,9 @@ connect_context_step (ConnectContext *ctx)
>              qmi_client_wds_set_ip_family (ctx->client_ipv4,
>                                            input,
>                                            10,
> -                                          ctx->cancellable,
> +                                          cancellable,
>                                            (GAsyncReadyCallback)set_ip_family_ready,
> -                                          ctx);
> +                                          task);
>              qmi_message_wds_set_ip_family_input_unref (input);
>              return;
>          }
> @@ -1144,9 +1156,9 @@ connect_context_step (ConnectContext *ctx)
>                                                                         &ctx->packet_service_status_ipv4_indication_id);
>          setup_event_report_unsolicited_events (ctx->self,
>                                                 ctx->client_ipv4,
> -                                               ctx->cancellable,
> +                                               cancellable,
>                                                 (GAsyncReadyCallback) connect_enable_indications_ipv4_ready,
> -                                               ctx);
> +                                               task);
>          return;
>  
>      case CONNECT_STEP_START_NETWORK_IPV4: {
> @@ -1157,9 +1169,9 @@ connect_context_step (ConnectContext *ctx)
>          qmi_client_wds_start_network (ctx->client_ipv4,
>                                        input,
>                                        45,
> -                                      ctx->cancellable,
> +                                      cancellable,
>                                        (GAsyncReadyCallback)start_network_ready,
> -                                      ctx);
> +                                      task);
>          qmi_message_wds_start_network_input_unref (input);
>          return;
>      }
> @@ -1168,7 +1180,7 @@ connect_context_step (ConnectContext *ctx)
>          /* Retrieve and print IP configuration */
>          if (ctx->packet_data_handle_ipv4) {
>              mm_dbg ("Getting IPv4 configuration...");
> -            get_current_settings (ctx, ctx->client_ipv4);
> +            get_current_settings (task, ctx->client_ipv4);
>              return;
>          }
>          /* Fall through */
> @@ -1179,7 +1191,7 @@ connect_context_step (ConnectContext *ctx)
>          /* If no IPv6 setup needed, jump to last */
>          if (!ctx->ipv6) {
>              ctx->step = CONNECT_STEP_LAST;
> -            connect_context_step (ctx);
> +            connect_context_step (task);
>              return;
>          }
>  
> @@ -1201,9 +1213,9 @@ connect_context_step (ConnectContext *ctx)
>              mm_port_qmi_allocate_client (ctx->qmi,
>                                           QMI_SERVICE_WDS,
>                                           MM_PORT_QMI_FLAG_WDS_IPV6,
> -                                         ctx->cancellable,
> +                                         cancellable,
>                                           (GAsyncReadyCallback)qmi_port_allocate_client_ready,
> -                                         ctx);
> +                                         task);
>              return;
>          }
>  
> @@ -1226,9 +1238,9 @@ connect_context_step (ConnectContext *ctx)
>              qmi_client_wds_set_ip_family (ctx->client_ipv6,
>                                            input,
>                                            10,
> -                                          ctx->cancellable,
> +                                          cancellable,
>                                            (GAsyncReadyCallback)set_ip_family_ready,
> -                                          ctx);
> +                                          task);
>              qmi_message_wds_set_ip_family_input_unref (input);
>              return;
>          }
> @@ -1245,9 +1257,9 @@ connect_context_step (ConnectContext *ctx)
>                                                                         &ctx->packet_service_status_ipv6_indication_id);
>          setup_event_report_unsolicited_events (ctx->self,
>                                                 ctx->client_ipv6,
> -                                               ctx->cancellable,
> +                                               cancellable,
>                                                 (GAsyncReadyCallback) connect_enable_indications_ipv6_ready,
> -                                               ctx);
> +                                               task);
>          return;
>  
>      case CONNECT_STEP_START_NETWORK_IPV6: {
> @@ -1258,9 +1270,9 @@ connect_context_step (ConnectContext *ctx)
>          qmi_client_wds_start_network (ctx->client_ipv6,
>                                        input,
>                                        45,
> -                                      ctx->cancellable,
> +                                      cancellable,
>                                        (GAsyncReadyCallback)start_network_ready,
> -                                      ctx);
> +                                      task);
>          qmi_message_wds_start_network_input_unref (input);
>          return;
>      }
> @@ -1269,7 +1281,7 @@ connect_context_step (ConnectContext *ctx)
>          /* Retrieve and print IP configuration */
>          if (ctx->packet_data_handle_ipv6) {
>              mm_dbg ("Getting IPv6 configuration...");
> -            get_current_settings (ctx, ctx->client_ipv6);
> +            get_current_settings (task, ctx->client_ipv6);
>              return;
>          }
>          /* Fall through */
> @@ -1309,8 +1321,8 @@ connect_context_step (ConnectContext *ctx)
>              }
>  
>              /* Set operation result */
> -            g_simple_async_result_set_op_res_gpointer (
> -                ctx->result,
> +            g_task_return_pointer (
> +                task,
>                  mm_bearer_connect_result_new (ctx->data, ctx->ipv4_config, ctx->ipv6_config),
>                  (GDestroyNotify)mm_bearer_connect_result_unref);
>          } else {
> @@ -1325,10 +1337,10 @@ connect_context_step (ConnectContext *ctx)
>                  ctx->error_ipv6 = NULL;
>              }
>  
> -            g_simple_async_result_take_error (ctx->result, error);
> +            g_task_return_error (task, error);
>          }
>  
> -        connect_context_complete_and_free (ctx);
> +        g_object_unref (task);
>          return;
>      }
>  }
> @@ -1346,6 +1358,7 @@ _connect (MMBaseBearer *self,
>      MMPortQmi *qmi;
>      GError *error = NULL;
>      const gchar *apn;
> +    GTask *task;
>  
>      g_object_get (self,
>                    MM_BASE_BEARER_MODEM, &modem,
> @@ -1355,10 +1368,11 @@ _connect (MMBaseBearer *self,
>      /* Grab a data port */
>      data = mm_base_modem_get_best_data_port (modem, MM_PORT_TYPE_NET);
>      if (!data) {
> -        g_simple_async_report_error_in_idle (
> -            G_OBJECT (self),
> +        g_task_report_new_error (
> +            self,
>              callback,
>              user_data,
> +            _connect,
>              MM_CORE_ERROR,
>              MM_CORE_ERROR_NOT_FOUND,
>              "No valid data port found to launch connection");
> @@ -1369,10 +1383,11 @@ _connect (MMBaseBearer *self,
>      /* Each data port has a single QMI port associated */
>      qmi = mm_base_modem_get_port_qmi_for_data (modem, data, &error);
>      if (!qmi) {
> -        g_simple_async_report_take_gerror_in_idle (
> -            G_OBJECT (self),
> +        g_task_report_error (
> +            self,
>              callback,
>              user_data,
> +            _connect,
>              error);
>          g_object_unref (data);
>          g_object_unref (modem);
> @@ -1384,10 +1399,11 @@ _connect (MMBaseBearer *self,
>  
>      /* Is this a 3GPP only modem and no APN was given? If so, error */
>      if (mm_iface_modem_is_3gpp_only (MM_IFACE_MODEM (modem)) && !apn) {
> -        g_simple_async_report_error_in_idle (
> -            G_OBJECT (self),
> +        g_task_report_new_error (
> +            self,
>              callback,
>              user_data,
> +            _connect,
>              MM_CORE_ERROR,
>              MM_CORE_ERROR_INVALID_ARGS,
>              "3GPP connection logic requires APN setting");
> @@ -1397,10 +1413,11 @@ _connect (MMBaseBearer *self,
>  
>      /* Is this a 3GPP2 only modem and APN was given? If so, error */
>      if (mm_iface_modem_is_cdma_only (MM_IFACE_MODEM (modem)) && apn) {
> -        g_simple_async_report_error_in_idle (
> -            G_OBJECT (self),
> +        g_task_report_new_error (
> +            self,
>              callback,
>              user_data,
> +            _connect,
>              MM_CORE_ERROR,
>              MM_CORE_ERROR_INVALID_ARGS,
>              "3GPP2 doesn't support APN setting");
> @@ -1420,18 +1437,16 @@ _connect (MMBaseBearer *self,
>      ctx->self = g_object_ref (self);
>      ctx->qmi = qmi;
>      ctx->data = data;
> -    ctx->cancellable = g_object_ref (cancellable);
>      ctx->step = CONNECT_STEP_FIRST;
>      ctx->ip_method = MM_BEARER_IP_METHOD_UNKNOWN;
> -    ctx->result = g_simple_async_result_new (G_OBJECT (self),
> -                                             callback,
> -                                             user_data,
> -                                             connect);
>  
>      g_object_get (self,
>                    MM_BASE_BEARER_CONFIG, &properties,
>                    NULL);
>  
> +    task = g_task_new (self, cancellable, callback, user_data);
> +    g_task_set_task_data (task, ctx, (GDestroyNotify)connect_context_free);
> +
>      if (properties) {
>          MMBearerAllowedAuth auth;
>          MMBearerIpFamily ip_family;
> @@ -1466,14 +1481,14 @@ _connect (MMBaseBearer *self,
>              gchar *str;
>  
>              str = mm_bearer_ip_family_build_string_from_mask (ip_family);
> -            g_simple_async_result_set_error (
> -                ctx->result,
> +            g_task_return_new_error (
> +                task,
>                  MM_CORE_ERROR,
>                  MM_CORE_ERROR_UNSUPPORTED,
>                  "Unsupported IP type requested: '%s'",
>                  str);
> +            g_object_unref (task);
>              g_free (str);
> -            connect_context_complete_and_free (ctx);
>              return;
>          }
>  
> @@ -1492,20 +1507,20 @@ _connect (MMBaseBearer *self,
>              gchar *str;
>  
>              str = mm_bearer_allowed_auth_build_string_from_mask (auth);
> -            g_simple_async_result_set_error (
> -                ctx->result,
> +            g_task_return_new_error (
> +                task,
>                  MM_CORE_ERROR,
>                  MM_CORE_ERROR_UNSUPPORTED,
>                  "Cannot use any of the specified authentication methods (%s)",
>                  str);
> +            g_object_unref (task);
>              g_free (str);
> -            connect_context_complete_and_free (ctx);
>              return;
>          }
>      }
>  
>      /* Run! */
> -    connect_context_step (ctx);
> +    connect_context_step (task);
>  }
>  
>  /*****************************************************************************/
> 


-- 
Aleksander
https://aleksander.es


More information about the ModemManager-devel mailing list