hso: port {enable,disable}_location_gathering to use GTask

This commit is contained in:
Ben Chan
2017-09-19 03:19:21 -07:00
committed by Aleksander Morgado
parent 227eae3f31
commit 8bae1a785a

View File

@@ -406,20 +406,9 @@ location_load_capabilities (MMIfaceModemLocation *self,
/* Enable/Disable location gathering (Location interface) */ /* Enable/Disable location gathering (Location interface) */
typedef struct { typedef struct {
MMBroadbandModemHso *self;
GSimpleAsyncResult *result;
MMModemLocationSource source; MMModemLocationSource source;
} LocationGatheringContext; } LocationGatheringContext;
static void
location_gathering_context_complete_and_free (LocationGatheringContext *ctx)
{
g_simple_async_result_complete_in_idle (ctx->result);
g_object_unref (ctx->result);
g_object_unref (ctx->self);
g_slice_free (LocationGatheringContext, ctx);
}
/******************************/ /******************************/
/* Disable location gathering */ /* Disable location gathering */
@@ -428,21 +417,21 @@ disable_location_gathering_finish (MMIfaceModemLocation *self,
GAsyncResult *res, GAsyncResult *res,
GError **error) GError **error)
{ {
return !g_simple_async_result_propagate_error (G_SIMPLE_ASYNC_RESULT (res), error); return g_task_propagate_boolean (G_TASK (res), error);
} }
static void static void
gps_disabled_ready (MMBaseModem *self, gps_disabled_ready (MMBaseModem *self,
GAsyncResult *res, GAsyncResult *res,
LocationGatheringContext *ctx) GTask *task)
{ {
LocationGatheringContext *ctx;
MMPortSerialGps *gps_port; MMPortSerialGps *gps_port;
GError *error = NULL; GError *error = NULL;
if (!mm_base_modem_at_command_full_finish (self, res, &error)) mm_base_modem_at_command_full_finish (self, res, &error);
g_simple_async_result_take_error (ctx->result, error);
else ctx = g_task_get_task_data (task);
g_simple_async_result_set_op_res_gboolean (ctx->result, TRUE);
/* Only use the GPS port in NMEA/RAW setups */ /* Only use the GPS port in NMEA/RAW setups */
if (ctx->source & (MM_MODEM_LOCATION_SOURCE_GPS_NMEA | if (ctx->source & (MM_MODEM_LOCATION_SOURCE_GPS_NMEA |
@@ -453,7 +442,12 @@ gps_disabled_ready (MMBaseModem *self,
mm_port_serial_close (MM_PORT_SERIAL (gps_port)); mm_port_serial_close (MM_PORT_SERIAL (gps_port));
} }
location_gathering_context_complete_and_free (ctx); if (error)
g_task_return_error (task, error);
else
g_task_return_boolean (task, TRUE);
g_object_unref (task);
} }
static void static void
@@ -465,15 +459,14 @@ disable_location_gathering (MMIfaceModemLocation *self,
MMBroadbandModemHso *hso = MM_BROADBAND_MODEM_HSO (self); MMBroadbandModemHso *hso = MM_BROADBAND_MODEM_HSO (self);
gboolean stop_gps = FALSE; gboolean stop_gps = FALSE;
LocationGatheringContext *ctx; LocationGatheringContext *ctx;
GTask *task;
ctx = g_slice_new (LocationGatheringContext); ctx = g_new (LocationGatheringContext, 1);
ctx->self = g_object_ref (self);
ctx->result = g_simple_async_result_new (G_OBJECT (self),
callback,
user_data,
disable_location_gathering);
ctx->source = source; ctx->source = source;
task = g_task_new (self, NULL, callback, user_data);
g_task_set_task_data (task, ctx, g_free);
/* Only stop GPS engine if no GPS-related sources enabled */ /* Only stop GPS engine if no GPS-related sources enabled */
if (source & (MM_MODEM_LOCATION_SOURCE_GPS_NMEA | if (source & (MM_MODEM_LOCATION_SOURCE_GPS_NMEA |
MM_MODEM_LOCATION_SOURCE_GPS_RAW | MM_MODEM_LOCATION_SOURCE_GPS_RAW |
@@ -496,13 +489,13 @@ disable_location_gathering (MMIfaceModemLocation *self,
FALSE, /* raw */ FALSE, /* raw */
NULL, /* cancellable */ NULL, /* cancellable */
(GAsyncReadyCallback)gps_disabled_ready, (GAsyncReadyCallback)gps_disabled_ready,
ctx); task);
return; return;
} }
/* For any other location (e.g. 3GPP), or if still some GPS needed, just return */ /* For any other location (e.g. 3GPP), or if still some GPS needed, just return */
g_simple_async_result_set_op_res_gboolean (ctx->result, TRUE); g_task_return_boolean (task, TRUE);
location_gathering_context_complete_and_free (ctx); g_object_unref (task);
} }
/*****************************/ /*****************************/
@@ -513,22 +506,25 @@ enable_location_gathering_finish (MMIfaceModemLocation *self,
GAsyncResult *res, GAsyncResult *res,
GError **error) GError **error)
{ {
return !g_simple_async_result_propagate_error (G_SIMPLE_ASYNC_RESULT (res), error); return g_task_propagate_boolean (G_TASK (res), error);
} }
static void static void
gps_enabled_ready (MMBaseModem *self, gps_enabled_ready (MMBaseModem *self,
GAsyncResult *res, GAsyncResult *res,
LocationGatheringContext *ctx) GTask *task)
{ {
LocationGatheringContext *ctx;
GError *error = NULL; GError *error = NULL;
if (!mm_base_modem_at_command_full_finish (self, res, &error)) { if (!mm_base_modem_at_command_full_finish (self, res, &error)) {
g_simple_async_result_take_error (ctx->result, error); g_task_return_error (task, error);
location_gathering_context_complete_and_free (ctx); g_object_unref (task);
return; return;
} }
ctx = g_task_get_task_data (task);
/* Only use the GPS port in NMEA/RAW setups */ /* Only use the GPS port in NMEA/RAW setups */
if (ctx->source & (MM_MODEM_LOCATION_SOURCE_GPS_NMEA | if (ctx->source & (MM_MODEM_LOCATION_SOURCE_GPS_NMEA |
MM_MODEM_LOCATION_SOURCE_GPS_RAW)) { MM_MODEM_LOCATION_SOURCE_GPS_RAW)) {
@@ -538,37 +534,41 @@ gps_enabled_ready (MMBaseModem *self,
if (!gps_port || if (!gps_port ||
!mm_port_serial_open (MM_PORT_SERIAL (gps_port), &error)) { !mm_port_serial_open (MM_PORT_SERIAL (gps_port), &error)) {
if (error) if (error)
g_simple_async_result_take_error (ctx->result, error); g_task_return_error (task, error);
else else
g_simple_async_result_set_error (ctx->result, g_task_return_new_error (task,
MM_CORE_ERROR, MM_CORE_ERROR,
MM_CORE_ERROR_FAILED, MM_CORE_ERROR_FAILED,
"Couldn't open raw GPS serial port"); "Couldn't open raw GPS serial port");
} else } else
g_simple_async_result_set_op_res_gboolean (ctx->result, TRUE); g_task_return_boolean (task, TRUE);
} }
else else
g_simple_async_result_set_op_res_gboolean (ctx->result, TRUE); g_task_return_boolean (task, TRUE);
location_gathering_context_complete_and_free (ctx); g_object_unref (task);
} }
static void static void
parent_enable_location_gathering_ready (MMIfaceModemLocation *self, parent_enable_location_gathering_ready (MMIfaceModemLocation *_self,
GAsyncResult *res, GAsyncResult *res,
LocationGatheringContext *ctx) GTask *task)
{ {
MMBroadbandModemHso *self = MM_BROADBAND_MODEM_HSO (_self);
LocationGatheringContext *ctx;
gboolean start_gps = FALSE; gboolean start_gps = FALSE;
GError *error = NULL; GError *error = NULL;
if (!iface_modem_location_parent->enable_location_gathering_finish (self, res, &error)) { if (!iface_modem_location_parent->enable_location_gathering_finish (_self, res, &error)) {
g_simple_async_result_take_error (ctx->result, error); g_task_return_error (task, error);
location_gathering_context_complete_and_free (ctx); g_object_unref (task);
return; return;
} }
/* Now our own enabling */ /* Now our own enabling */
ctx = g_task_get_task_data (task);
/* NMEA, RAW and UNMANAGED are all enabled in the same way */ /* NMEA, RAW and UNMANAGED are all enabled in the same way */
if (ctx->source & (MM_MODEM_LOCATION_SOURCE_GPS_NMEA | if (ctx->source & (MM_MODEM_LOCATION_SOURCE_GPS_NMEA |
MM_MODEM_LOCATION_SOURCE_GPS_RAW | MM_MODEM_LOCATION_SOURCE_GPS_RAW |
@@ -576,11 +576,11 @@ parent_enable_location_gathering_ready (MMIfaceModemLocation *self,
/* Only start GPS engine if not done already. /* Only start GPS engine if not done already.
* NOTE: interface already takes care of making sure that raw/nmea and * NOTE: interface already takes care of making sure that raw/nmea and
* unmanaged are not enabled at the same time */ * unmanaged are not enabled at the same time */
if (!(ctx->self->priv->enabled_sources & (MM_MODEM_LOCATION_SOURCE_GPS_NMEA | if (!(self->priv->enabled_sources & (MM_MODEM_LOCATION_SOURCE_GPS_NMEA |
MM_MODEM_LOCATION_SOURCE_GPS_RAW | MM_MODEM_LOCATION_SOURCE_GPS_RAW |
MM_MODEM_LOCATION_SOURCE_GPS_UNMANAGED))) MM_MODEM_LOCATION_SOURCE_GPS_UNMANAGED)))
start_gps = TRUE; start_gps = TRUE;
ctx->self->priv->enabled_sources |= ctx->source; self->priv->enabled_sources |= ctx->source;
} }
if (start_gps) { if (start_gps) {
@@ -593,13 +593,13 @@ parent_enable_location_gathering_ready (MMIfaceModemLocation *self,
FALSE, /* raw */ FALSE, /* raw */
NULL, /* cancellable */ NULL, /* cancellable */
(GAsyncReadyCallback)gps_enabled_ready, (GAsyncReadyCallback)gps_enabled_ready,
ctx); task);
return; return;
} }
/* For any other location (e.g. 3GPP), or if GPS already running just return */ /* For any other location (e.g. 3GPP), or if GPS already running just return */
g_simple_async_result_set_op_res_gboolean (ctx->result, TRUE); g_task_return_boolean (task, TRUE);
location_gathering_context_complete_and_free (ctx); g_object_unref (task);
} }
static void static void
@@ -609,21 +609,20 @@ enable_location_gathering (MMIfaceModemLocation *self,
gpointer user_data) gpointer user_data)
{ {
LocationGatheringContext *ctx; LocationGatheringContext *ctx;
GTask *task;
ctx = g_slice_new (LocationGatheringContext); ctx = g_new (LocationGatheringContext, 1);
ctx->self = g_object_ref (self);
ctx->result = g_simple_async_result_new (G_OBJECT (self),
callback,
user_data,
enable_location_gathering);
ctx->source = source; ctx->source = source;
task = g_task_new (self, NULL, callback, user_data);
g_task_set_task_data (task, ctx, g_free);
/* Chain up parent's gathering enable */ /* Chain up parent's gathering enable */
iface_modem_location_parent->enable_location_gathering ( iface_modem_location_parent->enable_location_gathering (
self, self,
source, source,
(GAsyncReadyCallback)parent_enable_location_gathering_ready, (GAsyncReadyCallback)parent_enable_location_gathering_ready,
ctx); task);
} }
/*****************************************************************************/ /*****************************************************************************/