diff options
-rw-r--r-- | cli/qmicli-dms.c | 65 | ||||
-rw-r--r-- | cli/qmicli-wds.c | 66 | ||||
-rw-r--r-- | cli/qmicli.c | 99 | ||||
-rw-r--r-- | cli/qmicli.h | 2 |
4 files changed, 105 insertions, 127 deletions
diff --git a/cli/qmicli-dms.c b/cli/qmicli-dms.c index 9586a1e..391a77a 100644 --- a/cli/qmicli-dms.c +++ b/cli/qmicli-dms.c @@ -102,36 +102,14 @@ context_free (Context *ctx) } static void -release_client_ready (QmiDevice *device, - GAsyncResult *res) +shutdown (void) { - GError *error = NULL; - - if (!qmi_device_release_client_finish (device, res, &error)) { - g_printerr ("error: couldn't release client: %s", error->message); - exit (EXIT_FAILURE); - } - - g_debug ("Client released"); - /* Cleanup context and finish async operation */ context_free (ctx); qmicli_async_operation_done (); } static void -shutdown (void) -{ - qmi_device_release_client (ctx->device, - QMI_CLIENT (ctx->client), - QMI_DEVICE_RELEASE_CLIENT_FLAGS_RELEASE_CID, - 10, - NULL, - (GAsyncReadyCallback)release_client_ready, - NULL); -} - -static void get_ids_ready (QmiClientDms *client, GAsyncResult *res) { @@ -166,18 +144,17 @@ get_ids_ready (QmiClientDms *client, shutdown (); } -static void -allocate_client_ready (QmiDevice *device, - GAsyncResult *res) +void +qmicli_dms_run (QmiDevice *device, + QmiClientDms *client, + GCancellable *cancellable) { - GError *error = NULL; - - ctx->client = (QmiClientDms *)qmi_device_allocate_client_finish (device, res, &error); - if (!ctx->client) { - g_printerr ("error: couldn't create DMS client: %s\n", - error->message); - exit (EXIT_FAILURE); - } + /* Initialize context */ + ctx = g_slice_new (Context); + ctx->device = g_object_ref (device); + ctx->client = g_object_ref (client); + if (cancellable) + ctx->cancellable = g_object_ref (cancellable); /* Request to get IDs? */ if (get_ids_flag) { @@ -192,23 +169,3 @@ allocate_client_ready (QmiDevice *device, g_warn_if_reached (); } - -void -qmicli_dms_run (QmiDevice *device, - GCancellable *cancellable) -{ - /* Initialize context */ - ctx = g_slice_new (Context); - ctx->device = g_object_ref (device); - if (cancellable) - ctx->cancellable = g_object_ref (cancellable); - - /* Create a new DMS client */ - qmi_device_allocate_client (device, - QMI_SERVICE_DMS, - QMI_CID_NONE, - 10, - cancellable, - (GAsyncReadyCallback)allocate_client_ready, - NULL); -} diff --git a/cli/qmicli-wds.c b/cli/qmicli-wds.c index dc367be..97fa5ec 100644 --- a/cli/qmicli-wds.c +++ b/cli/qmicli-wds.c @@ -127,36 +127,14 @@ context_free (Context *ctx) } static void -release_client_ready (QmiDevice *device, - GAsyncResult *res) +shutdown (void) { - GError *error = NULL; - - if (!qmi_device_release_client_finish (device, res, &error)) { - g_printerr ("error: couldn't release client: %s", error->message); - exit (EXIT_FAILURE); - } - - g_debug ("Client released"); - /* Cleanup context and finish async operation */ context_free (ctx); qmicli_async_operation_done (); } static void -shutdown (void) -{ - qmi_device_release_client (ctx->device, - QMI_CLIENT (ctx->client), - QMI_DEVICE_RELEASE_CLIENT_FLAGS_RELEASE_CID, - 10, - NULL, - (GAsyncReadyCallback)release_client_ready, - NULL); -} - -static void stop_network_ready (QmiClientWds *client, GAsyncResult *res) { @@ -498,18 +476,19 @@ get_current_data_bearer_technology_ready (QmiClientWds *client, shutdown (); } -static void -allocate_client_ready (QmiDevice *device, - GAsyncResult *res) +void +qmicli_wds_run (QmiDevice *device, + QmiClientWds *client, + GCancellable *cancellable) { - GError *error = NULL; + /* Initialize context */ + ctx = g_slice_new (Context); + ctx->device = g_object_ref (device); + ctx->client = g_object_ref (client); + ctx->cancellable = g_object_ref (cancellable); + ctx->network_started_id = 0; + ctx->packet_status_timeout_id = 0; - ctx->client = (QmiClientWds *)qmi_device_allocate_client_finish (device, res, &error); - if (!ctx->client) { - g_printerr ("error: couldn't create WDS client: %s\n", - error->message); - exit (EXIT_FAILURE); - } /* Request to start network? */ if (start_network_flag) { @@ -561,24 +540,3 @@ allocate_client_ready (QmiDevice *device, g_warn_if_reached (); } - -void -qmicli_wds_run (QmiDevice *device, - GCancellable *cancellable) -{ - /* Initialize context */ - ctx = g_slice_new (Context); - ctx->device = g_object_ref (device); - ctx->cancellable = g_object_ref (cancellable); - ctx->network_started_id = 0; - ctx->packet_status_timeout_id = 0; - - /* Create a new WDS client */ - qmi_device_allocate_client (device, - QMI_SERVICE_WDS, - QMI_CID_NONE, - 10, - cancellable, - (GAsyncReadyCallback)allocate_client_ready, - NULL); -} diff --git a/cli/qmicli.c b/cli/qmicli.c index 79af7e0..8d95206 100644 --- a/cli/qmicli.c +++ b/cli/qmicli.c @@ -39,6 +39,8 @@ static GMainLoop *loop; static GCancellable *cancellable; static QmiDevice *device; +static QmiClient *client; +static QmiService service; /* Main options */ static gchar *device_str; @@ -146,16 +148,66 @@ print_version_and_exit (void) /*****************************************************************************/ /* Running asynchronously */ +static void +release_client_ready (QmiDevice *device, + GAsyncResult *res) +{ + GError *error = NULL; + + if (!qmi_device_release_client_finish (device, res, &error)) { + g_printerr ("error: couldn't release client: %s", error->message); + exit (EXIT_FAILURE); + } + + g_debug ("Client released"); + g_main_loop_quit (loop); +} + void qmicli_async_operation_done (void) { + g_debug ("Asynchronous operation done..."); + if (cancellable) { g_object_unref (cancellable); cancellable = NULL; } - g_debug ("Asynchronous operation done... quitting the loop"); - g_main_loop_quit (loop); + qmi_device_release_client (device, + client, + QMI_DEVICE_RELEASE_CLIENT_FLAGS_RELEASE_CID, + 10, + NULL, + (GAsyncReadyCallback)release_client_ready, + NULL); +} + +static void +allocate_client_ready (QmiDevice *device, + GAsyncResult *res) +{ + + GError *error = NULL; + + client = qmi_device_allocate_client_finish (device, res, &error); + if (!client) { + g_printerr ("error: couldn't create client for the '%s' service: %s\n", + qmi_service_get_string (service), + error->message); + exit (EXIT_FAILURE); + } + + /* Run the service-specific action */ + switch (service) { + case QMI_SERVICE_DMS: + qmicli_dms_run (device, QMI_CLIENT_DMS (client), cancellable); + return; + case QMI_SERVICE_WDS: + qmicli_wds_run (device, QMI_CLIENT_WDS (client), cancellable); + return; + default: + g_assert_not_reached (); + } } static void @@ -170,25 +222,18 @@ device_open_ready (QmiDevice *device, exit (EXIT_FAILURE); } - /* As soon as we get the QmiDevice, launch the specific action requested */ g_debug ("QMI Device at '%s' ready", qmi_device_get_path_display (device)); - /* DMS options? */ - if (qmicli_dms_options_enabled ()) { - g_debug ("Running DMS operation..."); - qmicli_dms_run (device, cancellable); - } - /* WDS options? */ - else if (qmicli_wds_options_enabled ()) { - g_debug ("Running WDS operation..."); - qmicli_wds_run (device, cancellable); - } - /* No options? */ - else { - g_printerr ("error: no actions specified\n"); - exit (EXIT_FAILURE); - } + /* As soon as we get the QmiDevice, create a client for the requested + * service */ + qmi_device_allocate_client (device, + service, + QMI_CID_NONE, + 10, + cancellable, + (GAsyncReadyCallback)allocate_client_ready, + NULL); } static void @@ -262,6 +307,20 @@ int main (int argc, char **argv) signal (SIGHUP, signals_handler); signal (SIGTERM, signals_handler); + /* DMS options? */ + if (qmicli_dms_options_enabled ()) { + service = QMI_SERVICE_DMS; + } + /* WDS options? */ + else if (qmicli_wds_options_enabled ()) { + service = QMI_SERVICE_WDS; + } + /* No options? */ + else { + g_printerr ("error: no actions specified\n"); + exit (EXIT_FAILURE); + } + /* Create requirements for async options */ cancellable = g_cancellable_new (); loop = g_main_loop_new (NULL, FALSE); @@ -270,11 +329,13 @@ int main (int argc, char **argv) qmi_device_new (file, cancellable, (GAsyncReadyCallback)device_new_ready, - NULL); + GUINT_TO_POINTER (service)); g_main_loop_run (loop); if (cancellable) g_object_unref (cancellable); + if (client) + g_object_unref (client); if (device) g_object_unref (device); g_main_loop_unref (loop); diff --git a/cli/qmicli.h b/cli/qmicli.h index 71871ba..88f3355 100644 --- a/cli/qmicli.h +++ b/cli/qmicli.h @@ -30,12 +30,14 @@ void qmicli_async_operation_done (void); GOptionGroup *qmicli_dms_get_option_group (void); gboolean qmicli_dms_options_enabled (void); void qmicli_dms_run (QmiDevice *device, + QmiClientDms *client, GCancellable *cancellable); /* WDS group */ GOptionGroup *qmicli_wds_get_option_group (void); gboolean qmicli_wds_options_enabled (void); void qmicli_wds_run (QmiDevice *device, + QmiClientWds *client, GCancellable *cancellable); #endif /* __QMICLI_H__ */ |