[PATCH v2 1/1] qmicli: add support for the LOC service
Thomas Weißschuh
thomas at t-8ch.de
Wed Mar 21 19:28:55 UTC 2018
---
src/qmicli/Makefile.am | 1 +
src/qmicli/qmicli-loc.c | 515 ++++++++++++++++++++++++++++++++++++++++++++++++
src/qmicli/qmicli.c | 11 ++
src/qmicli/qmicli.h | 7 +
4 files changed, 534 insertions(+)
create mode 100644 src/qmicli/qmicli-loc.c
diff --git a/src/qmicli/Makefile.am b/src/qmicli/Makefile.am
index eb63fa7..8a4fefa 100644
--- a/src/qmicli/Makefile.am
+++ b/src/qmicli/Makefile.am
@@ -44,6 +44,7 @@ qmicli_SOURCES = \
qmicli-wms.c \
qmicli-wda.c \
qmicli-voice.c \
+ qmicli-loc.c \
qmicli-charsets.c \
qmicli-charsets.h
diff --git a/src/qmicli/qmicli-loc.c b/src/qmicli/qmicli-loc.c
new file mode 100644
index 0000000..fb915b6
--- /dev/null
+++ b/src/qmicli/qmicli-loc.c
@@ -0,0 +1,515 @@
+/* -*- Mode: C; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
+/*
+ * qmicli -- Command line interface to control QMI devices
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
+ *
+ * Copyright (C) 2018 Thomas Weißschuh <thomas at weissschuh.net>
+ */
+
+#include "config.h"
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <locale.h>
+#include <string.h>
+
+#include <glib.h>
+#include <gio/gio.h>
+
+#include <libqmi-glib.h>
+
+#include "qmicli.h"
+#include "qmicli-helpers.h"
+
+#define NO_TIMEOUT -1
+
+static gboolean show_position = FALSE;
+static gboolean show_satellites = FALSE;
+static gboolean show_nmea = FALSE;
+static gboolean start = FALSE;
+static gboolean stop = FALSE;
+static gboolean follow = FALSE;
+static gint session_id = 0x0;
+static gint timeout = NO_TIMEOUT;
+
+typedef enum {
+ UNDEFINED = 0,
+ REGISTER,
+ START,
+ PROCESS_INDICATIONS,
+ TIMEOUT,
+ STOP,
+ SHUTDOWN,
+} ProcessingState;
+
+typedef struct {
+ QmiClientLoc *client;
+ GCancellable *cancellable;
+ guint position_report_indication_id;
+ guint nmea_indication_id;
+ guint satellite_info_indication_id;
+ gboolean position_received;
+ gboolean satellites_received;
+ gboolean nmea_received;
+ ProcessingState state;
+} Context;
+
+static void next_step (Context *ctx);
+
+static GOptionEntry entries[] = {
+ {
+ "loc-session-id", 0, 0, G_OPTION_ARG_INT, &session_id,
+ "Session ID for LOC session",
+ NULL,
+ },
+ {
+ "loc-timeout", 0, 0, G_OPTION_ARG_INT, &timeout,
+ "Timeout for reception of indications",
+ NULL,
+ },
+ {
+ "loc-start", 0, 0, G_OPTION_ARG_NONE, &start,
+ "Start location gathering",
+ NULL,
+ },
+ {
+ "loc-stop", 0, 0, G_OPTION_ARG_NONE, &stop,
+ "Stop location gathering",
+ NULL,
+ },
+ {
+ "loc-follow", 0, 0, G_OPTION_ARG_NONE, &follow,
+ "Print follow updates",
+ NULL,
+ },
+ {
+ "loc-position-report", 0, 0, G_OPTION_ARG_NONE, &show_position,
+ "Show position",
+ NULL,
+ },
+ {
+ "loc-gnss-satellite-info", 0, 0, G_OPTION_ARG_NONE, &show_satellites,
+ "Show satellite report",
+ NULL,
+ },
+ {
+ "loc-nmea", 0, 0, G_OPTION_ARG_NONE, &show_nmea,
+ "Show NMEA string",
+ NULL,
+ },
+ { NULL }
+};
+
+GOptionGroup *
+qmicli_loc_get_option_group (void)
+{
+ GOptionGroup *group;
+
+ group = g_option_group_new ("loc",
+ "LOC options",
+ "Show location options", NULL, NULL);
+ g_option_group_add_entries (group, entries);
+
+ return group;
+}
+
+gboolean
+qmicli_loc_options_enabled (void)
+{
+ static gboolean options_valid = FALSE;
+ static gboolean checked = FALSE;
+
+ if (checked)
+ return options_valid;
+
+ options_valid = session_id >= 0 && session_id <= G_MAXUINT8;
+
+ if (!options_valid) {
+ g_printerr ("error: invalid value for session ID, (0 <= ID <= %d)\n",
+ G_MAXUINT8);
+ exit (EXIT_FAILURE);
+ }
+
+ options_valid = !(show_nmea && !follow);
+
+ if (!options_valid) {
+ g_printerr ("error: can only show NMEA in follow mode\n");
+ exit (EXIT_FAILURE);
+ }
+
+ checked = TRUE;
+ return options_valid;
+}
+
+static Context *
+context_new (QmiClientLoc *client, GCancellable *cancellable)
+{
+ Context *context;
+
+ context = g_slice_new0 (Context);
+ context->cancellable = g_object_ref (cancellable);
+ context->client = g_object_ref (client);
+ return context;
+}
+
+static void
+context_free (Context *context)
+{
+ if (!context)
+ return;
+
+ g_object_unref (context->client);
+ g_object_unref (context->cancellable);
+
+ g_slice_free (Context, context);
+}
+
+static void
+operation_shutdown (gboolean operation_status, Context *ctx)
+{
+ context_free (ctx);
+ qmicli_async_operation_done (operation_status, FALSE);
+}
+
+static void
+on_started (QmiClientLoc *client, GAsyncResult *res, Context *ctx)
+{
+ QmiMessageLocStartOutput *output;
+ GError *error = NULL;
+
+ output = qmi_client_loc_start_finish (client, res, &error);
+ if (output == NULL) {
+ g_printerr ("Could not start location tracking: %s\n", error->message);
+ goto error;
+ }
+
+ if (!qmi_message_loc_start_output_get_result (output, NULL)) {
+ g_printerr ("Could not start location tracking!\n");
+ goto error;
+ }
+
+ qmi_message_loc_start_output_unref (output);
+
+ next_step (ctx);
+ return;
+
+error:
+ qmi_message_loc_start_output_unref (output);
+ operation_shutdown (FALSE, ctx);
+}
+
+static void
+start_engine (Context *ctx) {
+ QmiMessageLocStartInput *input;
+
+ input = qmi_message_loc_start_input_new ();
+ qmi_message_loc_start_input_set_session_id (input, (guint8) session_id, NULL);
+ qmi_message_loc_start_input_set_intermediate_report_state (input, QMI_LOC_INTERMEDIATE_REPORT_STATE_ENABLE, NULL);
+ qmi_message_loc_start_input_set_minimum_interval_between_position_reports (input, 1000, NULL);
+ qmi_message_loc_start_input_set_fix_recurrence_type (input, QMI_LOC_FIX_RECURRENCE_TYPE_REQUEST_PERIODIC_FIXES, NULL);
+
+ qmi_client_loc_start (ctx->client, input, 10, ctx->cancellable, (GAsyncReadyCallback) on_started, ctx);
+}
+
+static void
+on_stopped (QmiClientLoc *client, GAsyncResult *res, Context *ctx)
+{
+ QmiMessageLocStopOutput *output;
+ GError *error = NULL;
+ gboolean success = TRUE;
+
+ output = qmi_client_loc_stop_finish (client, res, &error);
+ if (output == NULL) {
+ g_printerr ("Could not stop location tracking: %s\n", error->message);
+ goto error;
+ }
+
+ if (!qmi_message_loc_stop_output_get_result (output, NULL)) {
+ g_printerr ("Could not stop location tracking!\n");
+ goto error;
+ }
+
+out:
+ qmi_message_loc_stop_output_unref (output);
+ operation_shutdown (success, ctx);
+ next_step (ctx);
+ return;
+error:
+ success = FALSE;
+ goto out;
+}
+
+static void
+stop_engine (Context *ctx)
+{
+ QmiMessageLocStopInput *input;
+
+ input = qmi_message_loc_stop_input_new ();
+ qmi_message_loc_stop_input_set_session_id (input, (guint8) session_id, NULL);
+ qmi_client_loc_stop (ctx->client, input, 10, ctx->cancellable, (GAsyncReadyCallback) on_stopped, ctx);
+}
+
+static gboolean required_information_shown (Context *ctx)
+{
+ if (show_position && !ctx->position_received) {
+ return FALSE;
+ }
+ if (show_nmea && !ctx->nmea_received) {
+ return FALSE;
+ }
+ if (show_satellites && !ctx->satellites_received) {
+ return FALSE;
+ }
+ return TRUE;
+}
+
+static void
+report_received (Context *ctx)
+{
+ if (follow) {
+ return;
+ }
+
+ if (!required_information_shown (ctx)) {
+ return;
+ }
+
+ next_step (ctx);
+}
+
+static void
+position_report_received (QmiClientLoc *object,
+ QmiIndicationLocPositionReportOutput *output,
+ Context *ctx)
+{
+ gdouble lat, lon;
+ QmiLocSessionStatus status;
+
+ if (!follow && ctx->position_received) {
+ return;
+ }
+
+ qmi_indication_loc_position_report_output_get_latitude (output, &lat, NULL);
+ qmi_indication_loc_position_report_output_get_longitude (output, &lon, NULL);
+ qmi_indication_loc_position_report_output_get_session_status (output, &status, NULL);
+
+ if (status == QMI_LOC_SESSION_STATUS_SUCCESS) {
+ ctx->position_received = TRUE;
+ g_print ("Position: %f/%f\n", lat, lon);
+ report_received (ctx);
+ return;
+ }
+
+ if (status == QMI_LOC_SESSION_STATUS_IN_PROGRESS) {
+ g_print ("No position yet\n");
+ return;
+ }
+
+ g_print ("Error %s\n", qmi_loc_session_status_get_string (status));
+}
+
+static void
+nmea_received (QmiClientLoc *object,
+ QmiIndicationLocNmeaOutput *output,
+ Context *ctx)
+{
+ const gchar *nmea = NULL;
+
+ if (!follow && ctx->nmea_received) {
+ return;
+ }
+
+ ctx->nmea_received = TRUE;
+
+ qmi_indication_loc_nmea_output_get_nmea_string (output, &nmea, NULL);
+
+ if (nmea) {
+ g_print ("%s", nmea);
+ report_received (ctx);
+ }
+}
+
+static void
+satellite_info_received (QmiClientLoc *object,
+ QmiIndicationLocGnssSatelliteInfoOutput *output,
+ Context *ctx)
+{
+ GArray *satellite_infos = NULL;
+
+ if (!follow && ctx->satellites_received) {
+ return;
+ }
+
+ ctx->satellites_received = TRUE;
+
+ qmi_indication_loc_gnss_satellite_info_output_get_satellite_info (output, &satellite_infos, NULL);
+
+ g_print ("%d Satellites\n", satellite_infos ? satellite_infos->len : 0);
+ report_received (ctx);
+}
+
+static void
+cancel (GCancellable *cancellable, Context *ctx)
+{
+ /* gboolean success = !follow; */
+ if (ctx->state != STOP && ctx->state != SHUTDOWN) {
+ ctx->state = STOP;
+ }
+ next_step (ctx);
+}
+
+static void
+on_registered (QmiClientLoc *client, GAsyncResult *res, Context *ctx)
+{
+ QmiMessageLocRegisterEventsOutput *output;
+ GError *error = NULL;
+
+ output = qmi_client_loc_register_events_finish (client, res, &error);
+ if (output == NULL) {
+ g_printerr ("Could not register message listener: %s\n", error->message);
+ goto out;
+ }
+ if (!qmi_message_loc_register_events_output_get_result (output, NULL)) {
+ g_printerr ("Could not register message listener!\n");
+ goto out;
+ }
+
+ next_step (ctx);
+
+out:
+ qmi_message_loc_register_events_output_unref (output);
+}
+
+static void
+register_events (Context *ctx)
+{
+ QmiMessageLocRegisterEventsInput *i;
+ QmiLocEventRegistrationFlag indication_mask = 0;
+
+ if (show_position) {
+ indication_mask |= QMI_LOC_EVENT_REGISTRATION_FLAG_POSITION_REPORT;
+ }
+ if (show_satellites) {
+ indication_mask |= QMI_LOC_EVENT_REGISTRATION_FLAG_GNSS_SATELLITE_INFO;
+ }
+ if (show_nmea) {
+ indication_mask |= QMI_LOC_EVENT_REGISTRATION_FLAG_NMEA;
+ }
+
+ ctx->position_report_indication_id = g_signal_connect (ctx->client,
+ "position-report",
+ G_CALLBACK (position_report_received), ctx);
+ ctx->nmea_indication_id = g_signal_connect (ctx->client,
+ "nmea",
+ G_CALLBACK (nmea_received), ctx);
+ ctx->satellite_info_indication_id = g_signal_connect (ctx->client,
+ "gnss-satellite-info",
+ G_CALLBACK (satellite_info_received), ctx);
+
+ if (indication_mask) {
+ i = qmi_message_loc_register_events_input_new ();
+ qmi_message_loc_register_events_input_set_event_registration_mask (
+ i, indication_mask, NULL);
+ qmi_client_loc_register_events (
+ ctx->client, i, 10, ctx->cancellable,
+ (GAsyncReadyCallback) on_registered, ctx);
+ qmi_message_loc_register_events_input_unref (i);
+ } else {
+ next_step (ctx);
+ }
+}
+
+static gboolean
+on_timeout_reached (Context *ctx)
+{
+ ctx->state = TIMEOUT;
+ next_step (ctx);
+ return G_SOURCE_REMOVE;
+}
+
+static void
+next_step (Context *ctx)
+{
+ switch (ctx->state) {
+ case UNDEFINED:
+ g_printerr ("Undefined Application state\n");
+ operation_shutdown (FALSE, ctx);
+ return;
+ case REGISTER:
+ g_debug ("Registering location events");
+ ctx->state = START;
+ register_events (ctx);
+ return;
+ case START:
+ g_debug ("Starting location engine");
+ ctx->state = PROCESS_INDICATIONS;
+
+ if (start) {
+ start_engine (ctx);
+ return;
+ }
+ // fall-through
+ case PROCESS_INDICATIONS:
+ g_debug ("Processing location events");
+ ctx->state = STOP;
+ if (timeout != NO_TIMEOUT) {
+ g_timeout_add_seconds (timeout, (GSourceFunc) on_timeout_reached, ctx);
+ }
+ return;
+ case TIMEOUT:
+ if (!follow) {
+ g_printerr ("Timeout reached, exiting\n");
+ }
+ // fall-through
+ case STOP:
+ g_debug ("Stopping location engine");
+ ctx->state = SHUTDOWN;
+
+ if (stop) {
+ stop_engine (ctx);
+ return;
+ }
+ // fall-through
+ case SHUTDOWN:
+ g_debug ("Shutting down qmicli");
+ ctx->state = UNDEFINED;
+
+ operation_shutdown (TRUE, ctx);
+ return;
+ }
+}
+
+void
+qmicli_loc_run (QmiDevice *device,
+ QmiClientLoc *client,
+ GCancellable *cancellable)
+{
+
+ Context *ctx;
+
+ ctx = context_new (client, cancellable);
+
+ if (!follow && timeout == NO_TIMEOUT) {
+ timeout = 30;
+ }
+
+ ctx->state = REGISTER;
+
+ g_cancellable_connect (cancellable, G_CALLBACK (cancel), ctx, NULL);
+
+ next_step (ctx);
+
+ return;
+}
diff --git a/src/qmicli/qmicli.c b/src/qmicli/qmicli.c
index fecae8d..6993c9c 100644
--- a/src/qmicli/qmicli.c
+++ b/src/qmicli/qmicli.c
@@ -372,6 +372,9 @@ allocate_client_ready (QmiDevice *dev,
case QMI_SERVICE_VOICE:
qmicli_voice_run (dev, QMI_CLIENT_VOICE (client), cancellable);
return;
+ case QMI_SERVICE_LOC:
+ qmicli_loc_run (dev, QMI_CLIENT_LOC (client), cancellable);
+ return;
default:
g_assert_not_reached ();
}
@@ -731,6 +734,12 @@ parse_actions (void)
actions_enabled++;
}
+ /* LOC options? */
+ if (qmicli_loc_options_enabled ()) {
+ service = QMI_SERVICE_LOC;
+ actions_enabled++;
+ }
+
/* Cannot mix actions from different services */
if (actions_enabled > 1) {
g_printerr ("error: cannot execute multiple actions of different services\n");
@@ -774,6 +783,8 @@ int main (int argc, char **argv)
qmicli_wda_get_option_group ());
g_option_context_add_group (context,
qmicli_voice_get_option_group ());
+ g_option_context_add_group (context,
+ qmicli_loc_get_option_group ());
g_option_context_add_main_entries (context, main_entries, NULL);
if (!g_option_context_parse (context, &argc, &argv, &error)) {
g_printerr ("error: %s\n",
diff --git a/src/qmicli/qmicli.h b/src/qmicli/qmicli.h
index 7db7905..37b5f79 100644
--- a/src/qmicli/qmicli.h
+++ b/src/qmicli/qmicli.h
@@ -90,4 +90,11 @@ void qmicli_voice_run (QmiDevice *device,
QmiClientVoice *client,
GCancellable *cancellable);
+/* Location group */
+GOptionGroup *qmicli_loc_get_option_group (void);
+gboolean qmicli_loc_options_enabled (void);
+void qmicli_loc_run (QmiDevice *device,
+ QmiClientLoc *client,
+ GCancellable *cancellable);
+
#endif /* __QMICLI_H__ */
--
2.16.2
More information about the libqmi-devel
mailing list