/* -*- 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 .
*
* Copyright (C) 2018 Thomas Weißschuh
* Copyright (C) 2018 Aleksander Morgado
*/
#include "config.h"
#include
#include
#include
#include
#include
#include
#include
#include "qmicli.h"
#if defined HAVE_QMI_SERVICE_LOC
#undef VALIDATE_MASK_NONE
#define VALIDATE_MASK_NONE(str) (str ? str : "none")
/* Context */
typedef enum {
MONITORING_STEP_FIRST,
MONITORING_STEP_REGISTER_EVENTS,
MONITORING_STEP_SETUP_TIMEOUT,
MONITORING_STEP_ONGOING,
} MonitoringStep;
typedef struct {
QmiDevice *device;
QmiClientLoc *client;
GCancellable *cancellable;
guint timeout_id;
MonitoringStep monitoring_step;
guint nmea_indication_id;
} Context;
static Context *ctx;
/* Options */
static gint session_id;
static gboolean start_flag = 1;
static gboolean stop_flag;
static gint timeout;
static gboolean follow_nmea_flag;
static gboolean noop_flag;
#define DEFAULT_LOC_TIMEOUT_SECS 30
static GOptionEntry entries[] = {
{ NULL, 0, 0, 0, NULL, 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)
{
start_flag = !0;
follow_nmea_flag = 0;
g_debug("enabled %u %u", start_flag, follow_nmea_flag);
return start_flag;
}
static void
context_free (Context *context)
{
if (!context)
return;
if (context->timeout_id)
g_source_remove (context->timeout_id);
if (context->nmea_indication_id)
g_signal_handler_disconnect (context->client, context->nmea_indication_id);
g_clear_object (&context->cancellable);
g_clear_object (&context->client);
g_clear_object (&context->device);
g_slice_free (Context, context);
}
static void
operation_shutdown (gboolean operation_status)
{
context_free (ctx);
qmicli_async_operation_done (operation_status, FALSE);
}
#if (defined HAVE_QMI_INDICATION_LOC_POSITION_REPORT || \
defined HAVE_QMI_INDICATION_LOC_GNSS_SV_INFO || \
defined HAVE_QMI_INDICATION_LOC_NMEA) && \
defined HAVE_QMI_MESSAGE_LOC_REGISTER_EVENTS
static void monitoring_step_run (void);
static gboolean
monitoring_timed_out (void)
{
ctx->timeout_id = 0;
g_printerr ("error: operation failed: timeout\n");
operation_shutdown (FALSE);
return G_SOURCE_REMOVE;
}
static void
monitoring_cancelled (GCancellable *cancellable)
{
/* For FOLLOW operations, silently exit */
if (follow_nmea_flag) {
operation_shutdown (TRUE);
return;
}
g_assert_not_reached ();
}
#endif /* HAVE_QMI_INDICATION_LOC_POSITION_REPORT
* HAVE_QMI_INDICATION_LOC_GNSS_SV_INFO
* HAVE_QMI_INDICATION_LOC_NMEA */
#if defined HAVE_QMI_INDICATION_LOC_NMEA && defined HAVE_QMI_MESSAGE_LOC_REGISTER_EVENTS
static void
nmea_received (QmiClientLoc *client,
QmiIndicationLocNmeaOutput *output)
{
const gchar *nmea = NULL;
qmi_indication_loc_nmea_output_get_nmea_string (output, &nmea, NULL);
if (nmea)
/* Note: NMEA traces already have an EOL */
g_print ("%s", nmea);
}
#endif /* HAVE_QMI_INDICATION_LOC_NMEA */
#if (defined HAVE_QMI_INDICATION_LOC_POSITION_REPORT || \
defined HAVE_QMI_INDICATION_LOC_GNSS_SV_INFO || \
defined HAVE_QMI_INDICATION_LOC_NMEA) && \
defined HAVE_QMI_MESSAGE_LOC_REGISTER_EVENTS
static void
monitoring_step_ongoing (void)
{
#if defined HAVE_QMI_INDICATION_LOC_NMEA
if (follow_nmea_flag)
ctx->nmea_indication_id = g_signal_connect (ctx->client,
"nmea",
G_CALLBACK (nmea_received),
NULL);
#endif
g_assert (ctx->nmea_indication_id);
}
static void
monitoring_step_setup_timeout (void)
{
/* User can use Ctrl+C to cancel the monitoring at any time */
g_cancellable_connect (ctx->cancellable,
G_CALLBACK (monitoring_cancelled),
NULL,
NULL);
/* Go on */
ctx->monitoring_step++;
monitoring_step_run ();
}
static void
register_events_ready (QmiClientLoc *client,
GAsyncResult *res)
{
QmiMessageLocRegisterEventsOutput *output;
GError *error = NULL;
output = qmi_client_loc_register_events_finish (client, res, &error);
if (!output) {
g_printerr ("error: operation failed: %s\n", error->message);
g_error_free (error);
operation_shutdown (FALSE);
return;
}
if (!qmi_message_loc_register_events_output_get_result (output, &error)) {
g_printerr ("error: could not register location tracking events: %s\n", error->message);
qmi_message_loc_register_events_output_unref (output);
g_error_free (error);
operation_shutdown (FALSE);
return;
}
g_debug ("Registered location tracking events...");
/* Go on */
ctx->monitoring_step++;
monitoring_step_run ();
qmi_message_loc_register_events_output_unref (output);
}
static void
monitoring_step_register_events (void)
{
QmiMessageLocRegisterEventsInput *re_input;
QmiLocEventRegistrationFlag indication_mask = 0;
/* Configure events to enable */
if (follow_nmea_flag)
indication_mask |= QMI_LOC_EVENT_REGISTRATION_FLAG_NMEA;
g_assert (indication_mask);
re_input = qmi_message_loc_register_events_input_new ();
qmi_message_loc_register_events_input_set_event_registration_mask (
re_input, indication_mask, NULL);
qmi_client_loc_register_events (ctx->client,
re_input,
10,
ctx->cancellable,
(GAsyncReadyCallback) register_events_ready,
NULL);
qmi_message_loc_register_events_input_unref (re_input);
}
static void
monitoring_step_run (void)
{
switch (ctx->monitoring_step) {
case MONITORING_STEP_FIRST:
ctx->monitoring_step++;
/* fall through */
case MONITORING_STEP_REGISTER_EVENTS:
monitoring_step_register_events ();
return;
case MONITORING_STEP_SETUP_TIMEOUT:
monitoring_step_setup_timeout ();
return;
case MONITORING_STEP_ONGOING:
monitoring_step_ongoing ();
return;
default:
g_assert_not_reached();
}
}
#endif /* HAVE_QMI_INDICATION_LOC_POSITION_REPORT
* HAVE_QMI_INDICATION_LOC_GNSS_SV_INFO
* HAVE_QMI_INDICATION_LOC_NMEA */
#if defined HAVE_QMI_MESSAGE_LOC_STOP
static void
stop_ready (QmiClientLoc *client,
GAsyncResult *res)
{
QmiMessageLocStopOutput *output;
GError *error = NULL;
output = qmi_client_loc_stop_finish (client, res, &error);
if (!output) {
g_printerr ("error: operation failed: %s\n", error->message);
g_error_free (error);
operation_shutdown (FALSE);
return;
}
if (!qmi_message_loc_stop_output_get_result (output, &error)) {
g_printerr ("error: could not stop location tracking: %s\n", error->message);
qmi_message_loc_stop_output_unref (output);
g_error_free (error);
operation_shutdown (FALSE);
return;
}
g_print ("[%s] Successfully stopped location tracking (session id %u)\n",
qmi_device_get_path_display (ctx->device), session_id);
qmi_message_loc_stop_output_unref (output);
operation_shutdown (TRUE);
}
#endif /* HAVE_QMI_MESSAGE_LOC_STOP */
#if defined HAVE_QMI_MESSAGE_LOC_START
static void
start_ready (QmiClientLoc *client,
GAsyncResult *res)
{
QmiMessageLocStartOutput *output;
GError *error = NULL;
output = qmi_client_loc_start_finish (client, res, &error);
if (!output) {
g_printerr ("error: operation failed: %s\n", error->message);
g_error_free (error);
operation_shutdown (FALSE);
return;
}
if (!qmi_message_loc_start_output_get_result (output, &error)) {
g_printerr ("error: could not start location tracking: %s\n", error->message);
qmi_message_loc_start_output_unref (output);
g_error_free (error);
operation_shutdown (FALSE);
return;
}
g_print ("[%s] Successfully started location tracking (session id %u)\n",
qmi_device_get_path_display (ctx->device), session_id);
qmi_message_loc_start_output_unref (output);
context_free (ctx);
qmicli_async_operation_done (TRUE, TRUE);
}
#endif /* HAVE_QMI_MESSAGE_LOC_START */
static gboolean
noop_cb (gpointer unused)
{
operation_shutdown (TRUE);
return G_SOURCE_REMOVE;
}
void
qmicli_loc_run (QmiDevice *device,
QmiClientLoc *client,
GCancellable *cancellable)
{
/* Initialize context */
ctx = g_slice_new0 (Context);
ctx->device = g_object_ref (device);
ctx->client = g_object_ref (client);
ctx->cancellable = g_object_ref (cancellable);
g_debug("qmicli_loc_run start_flag %u stop-flag %u follow_nmea_flag %u",
start_flag, stop_flag, follow_nmea_flag);
#if defined HAVE_QMI_MESSAGE_LOC_START
if (start_flag) {
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) start_ready,
NULL);
qmi_message_loc_start_input_unref (input);
start_flag = 0;
follow_nmea_flag = 1;
g_debug("qmicli_loc_run returning, start_flag %u stop-flag %u follow_nmea_flag %u",
start_flag, stop_flag, follow_nmea_flag);
return;
}
#endif
#if defined HAVE_QMI_MESSAGE_LOC_STOP
if (stop_flag) {
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) stop_ready,
NULL);
qmi_message_loc_stop_input_unref (input);
return;
}
#endif
if (follow_nmea_flag) {
qmicli_expect_indications ();
/* All the remaining actions require monitoring */
ctx->monitoring_step = MONITORING_STEP_FIRST;
monitoring_step_run ();
g_debug("qmicli_loc_run returning, start_flag %u stop-flag %u follow_nmea_flag %u",
start_flag, stop_flag, follow_nmea_flag);
return;
}
/* Just client allocate/release? */
if (noop_flag) {
g_idle_add (noop_cb, NULL);
return;
}
g_warn_if_reached ();
}
#endif /* HAVE_QMI_SERVICE_LOC */