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) */
typedef struct {
MMBroadbandModemHuawei *self;
GSimpleAsyncResult *result;
MMModemLocationSource source;
int idx;
} 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 */
@@ -3726,21 +3715,21 @@ disable_location_gathering_finish (MMIfaceModemLocation *self,
GAsyncResult *res,
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
gps_disabled_ready (MMBaseModem *self,
GAsyncResult *res,
LocationGatheringContext *ctx)
GTask *task)
{
LocationGatheringContext *ctx;
MMPortSerialGps *gps_port;
GError *error = NULL;
if (!mm_base_modem_at_command_full_finish (self, res, &error))
g_simple_async_result_take_error (ctx->result, error);
else
g_simple_async_result_set_op_res_gboolean (ctx->result, TRUE);
ctx = g_task_get_task_data (task);
mm_base_modem_at_command_full_finish (self, res, &error);
/* Only use the GPS port in NMEA/RAW setups */
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));
}
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
@@ -3763,15 +3756,14 @@ disable_location_gathering (MMIfaceModemLocation *_self,
MMBroadbandModemHuawei *self = MM_BROADBAND_MODEM_HUAWEI (_self);
gboolean stop_gps = FALSE;
LocationGatheringContext *ctx;
GTask *task;
ctx = g_slice_new (LocationGatheringContext);
ctx->self = g_object_ref (self);
ctx->result = g_simple_async_result_new (G_OBJECT (self),
callback,
user_data,
disable_location_gathering);
ctx = g_new (LocationGatheringContext, 1);
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 */
if (source & (MM_MODEM_LOCATION_SOURCE_GPS_NMEA |
MM_MODEM_LOCATION_SOURCE_GPS_RAW |
@@ -3793,13 +3785,13 @@ disable_location_gathering (MMIfaceModemLocation *_self,
FALSE, /* raw */
NULL, /* cancellable */
(GAsyncReadyCallback)gps_disabled_ready,
ctx);
task);
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);
location_gathering_context_complete_and_free (ctx);
g_task_return_boolean (task, TRUE);
g_object_unref (task);
}
/*****************************************************************************/
@@ -3810,7 +3802,7 @@ enable_location_gathering_finish (MMIfaceModemLocation *self,
GAsyncResult *res,
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[] = {
@@ -3824,15 +3816,18 @@ static char *gps_startup[] = {
static void
gps_enabled_ready (MMBaseModem *self,
GAsyncResult *res,
LocationGatheringContext *ctx)
GTask *task)
{
LocationGatheringContext *ctx;
GError *error = NULL;
MMPortSerialGps *gps_port;
ctx = g_task_get_task_data (task);
if (!mm_base_modem_at_command_full_finish (self, res, &error)) {
ctx->idx = 0;
g_simple_async_result_take_error (ctx->result, error);
location_gathering_context_complete_and_free (ctx);
g_task_return_error (task, error);
g_object_unref (task);
return;
}
@@ -3846,7 +3841,7 @@ gps_enabled_ready (MMBaseModem *self,
FALSE, /* raw */
NULL, /* cancellable */
(GAsyncReadyCallback)gps_enabled_ready,
ctx);
task);
return;
}
@@ -3857,46 +3852,50 @@ gps_enabled_ready (MMBaseModem *self,
if (!gps_port ||
!mm_port_serial_open (MM_PORT_SERIAL (gps_port), &error)) {
if (error)
g_simple_async_result_take_error (ctx->result, error);
g_task_return_error (task, error);
else
g_simple_async_result_set_error (ctx->result,
g_task_return_new_error (task,
MM_CORE_ERROR,
MM_CORE_ERROR_FAILED,
"Couldn't open raw GPS serial port");
} else
g_simple_async_result_set_op_res_gboolean (ctx->result, TRUE);
g_task_return_boolean (task, TRUE);
} 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
parent_enable_location_gathering_ready (MMIfaceModemLocation *self,
parent_enable_location_gathering_ready (MMIfaceModemLocation *_self,
GAsyncResult *res,
LocationGatheringContext *ctx)
GTask *task)
{
MMBroadbandModemHuawei *self = MM_BROADBAND_MODEM_HUAWEI (_self);
LocationGatheringContext *ctx;
gboolean start_gps = FALSE;
GError *error = NULL;
if (!iface_modem_location_parent->enable_location_gathering_finish (self, res, &error)) {
g_simple_async_result_take_error (ctx->result, error);
location_gathering_context_complete_and_free (ctx);
if (!iface_modem_location_parent->enable_location_gathering_finish (_self, res, &error)) {
g_task_return_error (task, error);
g_object_unref (task);
return;
}
/* Now our own enabling */
ctx = g_task_get_task_data (task);
/* NMEA and RAW are both enabled in the same way */
if (ctx->source & (MM_MODEM_LOCATION_SOURCE_GPS_NMEA |
MM_MODEM_LOCATION_SOURCE_GPS_RAW |
MM_MODEM_LOCATION_SOURCE_GPS_UNMANAGED)) {
/* 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_UNMANAGED)))
start_gps = TRUE;
ctx->self->priv->enabled_sources |= ctx->source;
self->priv->enabled_sources |= ctx->source;
}
if (start_gps) {
@@ -3908,13 +3907,13 @@ parent_enable_location_gathering_ready (MMIfaceModemLocation *self,
FALSE, /* raw */
NULL, /* cancellable */
(GAsyncReadyCallback)gps_enabled_ready,
ctx);
task);
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);
location_gathering_context_complete_and_free (ctx);
g_task_return_boolean (task, TRUE);
g_object_unref (task);
}
static void
@@ -3924,21 +3923,20 @@ enable_location_gathering (MMIfaceModemLocation *self,
gpointer user_data)
{
LocationGatheringContext *ctx;
GTask *task;
ctx = g_slice_new (LocationGatheringContext);
ctx->self = g_object_ref (self);
ctx->result = g_simple_async_result_new (G_OBJECT (self),
callback,
user_data,
enable_location_gathering);
ctx = g_new (LocationGatheringContext, 1);
ctx->source = source;
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 */
iface_modem_location_parent->enable_location_gathering (self,
source,
(GAsyncReadyCallback)parent_enable_location_gathering_ready,
ctx);
task);
}
/*****************************************************************************/