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

This commit is contained in:
Ben Chan
2017-08-01 11:31:39 -07:00
committed by Aleksander Morgado
parent 6ebc07f882
commit 38bafe021e

View File

@@ -3703,21 +3703,10 @@ location_load_capabilities (MMIfaceModemLocation *self,
/* Enable/Disable location gathering (Location interface) */ /* Enable/Disable location gathering (Location interface) */
typedef struct { typedef struct {
MMBroadbandModemHuawei *self;
GSimpleAsyncResult *result;
MMModemLocationSource source; MMModemLocationSource source;
int idx; int idx;
} 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 */
@@ -3726,21 +3715,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)) ctx = g_task_get_task_data (task);
g_simple_async_result_take_error (ctx->result, error);
else mm_base_modem_at_command_full_finish (self, res, &error);
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 |
@@ -3751,7 +3740,11 @@ 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
@@ -3763,15 +3756,14 @@ disable_location_gathering (MMIfaceModemLocation *_self,
MMBroadbandModemHuawei *self = MM_BROADBAND_MODEM_HUAWEI (_self); MMBroadbandModemHuawei *self = MM_BROADBAND_MODEM_HUAWEI (_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 |
@@ -3793,13 +3785,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);
} }
/*****************************************************************************/ /*****************************************************************************/
@@ -3810,7 +3802,7 @@ 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 char *gps_startup[] = { static char *gps_startup[] = {
@@ -3824,15 +3816,18 @@ static char *gps_startup[] = {
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;
MMPortSerialGps *gps_port; MMPortSerialGps *gps_port;
ctx = g_task_get_task_data (task);
if (!mm_base_modem_at_command_full_finish (self, res, &error)) { if (!mm_base_modem_at_command_full_finish (self, res, &error)) {
ctx->idx = 0; ctx->idx = 0;
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;
} }
@@ -3846,7 +3841,7 @@ gps_enabled_ready (MMBaseModem *self,
FALSE, /* raw */ FALSE, /* raw */
NULL, /* cancellable */ NULL, /* cancellable */
(GAsyncReadyCallback)gps_enabled_ready, (GAsyncReadyCallback)gps_enabled_ready,
ctx); task);
return; return;
} }
@@ -3857,46 +3852,50 @@ 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)
{ {
MMBroadbandModemHuawei *self = MM_BROADBAND_MODEM_HUAWEI (_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 and RAW are both enabled in the same way */ /* NMEA and RAW are both 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 |
MM_MODEM_LOCATION_SOURCE_GPS_UNMANAGED)) { MM_MODEM_LOCATION_SOURCE_GPS_UNMANAGED)) {
/* Only start GPS engine if not done already */ /* Only start GPS engine if not done already */
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) {
@@ -3908,13 +3907,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
@@ -3924,21 +3923,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;
ctx->idx = 0; ctx->idx = 0;
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 (self, iface_modem_location_parent->enable_location_gathering (self,
source, source,
(GAsyncReadyCallback)parent_enable_location_gathering_ready, (GAsyncReadyCallback)parent_enable_location_gathering_ready,
ctx); task);
} }
/*****************************************************************************/ /*****************************************************************************/