huawei: minor refactor in GPS enabling/disabling logic

This commit is contained in:
Aleksander Morgado
2020-06-28 11:38:48 +02:00
parent 538ed3f24e
commit bbfc10d7c5

View File

@@ -3875,36 +3875,35 @@ location_load_capabilities (MMIfaceModemLocation *self,
typedef struct { typedef struct {
MMModemLocationSource source; MMModemLocationSource source;
int idx; guint idx;
} LocationGatheringContext; } LocationGatheringContext;
/******************************/ /******************************/
/* Disable location gathering */ /* Disable location gathering */
static gboolean static gboolean
disable_location_gathering_finish (MMIfaceModemLocation *self, disable_location_gathering_finish (MMIfaceModemLocation *self,
GAsyncResult *res, GAsyncResult *res,
GError **error) GError **error)
{ {
return g_task_propagate_boolean (G_TASK (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,
GTask *task) GTask *task)
{ {
LocationGatheringContext *ctx; LocationGatheringContext *ctx;
MMPortSerialGps *gps_port; MMPortSerialGps *gps_port;
GError *error = NULL; GError *error = NULL;
ctx = g_task_get_task_data (task); ctx = g_task_get_task_data (task);
mm_base_modem_at_command_full_finish (self, res, &error); mm_base_modem_at_command_full_finish (self, res, &error);
/* 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)) {
/* Even if we get an error here, we try to close the GPS port */ /* Even if we get an error here, we try to close the GPS port */
gps_port = mm_base_modem_peek_port_gps (self); gps_port = mm_base_modem_peek_port_gps (self);
if (gps_port) if (gps_port)
@@ -3919,15 +3918,15 @@ gps_disabled_ready (MMBaseModem *self,
} }
static void static void
disable_location_gathering (MMIfaceModemLocation *_self, disable_location_gathering (MMIfaceModemLocation *_self,
MMModemLocationSource source, MMModemLocationSource source,
GAsyncReadyCallback callback, GAsyncReadyCallback callback,
gpointer user_data) gpointer user_data)
{ {
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; GTask *task;
ctx = g_new (LocationGatheringContext, 1); ctx = g_new (LocationGatheringContext, 1);
ctx->source = source; ctx->source = source;
@@ -3968,58 +3967,66 @@ disable_location_gathering (MMIfaceModemLocation *_self,
/*****************************************************************************/ /*****************************************************************************/
/* Enable location gathering (Location interface) */ /* Enable location gathering (Location interface) */
static gboolean
enable_location_gathering_finish (MMIfaceModemLocation *self,
GAsyncResult *res,
GError **error)
{
return g_task_propagate_boolean (G_TASK (res), error);
}
static const gchar *gps_startup[] = { static const gchar *gps_startup[] = {
"^WPDOM=0", "^WPDOM=0",
"^WPDST=1", "^WPDST=1",
"^WPDFR=65535,30", "^WPDFR=65535,30",
"^WPDGP", "^WPDGP",
NULL
}; };
static void static gboolean
gps_enabled_ready (MMBaseModem *self, enable_location_gathering_finish (MMIfaceModemLocation *self,
GAsyncResult *res, GAsyncResult *res,
GTask *task) GError **error)
{
return g_task_propagate_boolean (G_TASK (res), error);
}
static void run_gps_startup (GTask *task);
static void
gps_startup_command_ready (MMBaseModem *self,
GAsyncResult *res,
GTask *task)
{ {
LocationGatheringContext *ctx;
GError *error = NULL; GError *error = NULL;
MMPortSerialGps *gps_port;
ctx = g_task_get_task_data (task); if (!mm_base_modem_at_command_finish (self, res, &error)) {
if (!mm_base_modem_at_command_full_finish (self, res, &error)) {
ctx->idx = 0;
g_task_return_error (task, error); g_task_return_error (task, error);
g_object_unref (task); g_object_unref (task);
return; return;
} }
/* ctx->idx++; make sure ctx->idx is a valid command */ /* continue with next command */
if (gps_startup[ctx->idx++] && gps_startup[ctx->idx]) { run_gps_startup (task);
mm_base_modem_at_command_full (MM_BASE_MODEM (self), }
mm_base_modem_peek_port_primary (MM_BASE_MODEM (self)),
gps_startup[ctx->idx], static void
3, run_gps_startup (GTask *task)
FALSE, {
FALSE, /* raw */ MMBroadbandModemHuawei *self;
NULL, /* cancellable */ LocationGatheringContext *ctx;
(GAsyncReadyCallback)gps_enabled_ready,
task); self = g_task_get_source_object (task);
ctx = g_task_get_task_data (task);
if (ctx->idx < G_N_ELEMENTS (gps_startup)) {
mm_base_modem_at_command (MM_BASE_MODEM (self),
gps_startup[ctx->idx++],
3,
FALSE,
(GAsyncReadyCallback)gps_startup_command_ready,
task);
return; return;
} }
/* 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)) {
gps_port = mm_base_modem_peek_port_gps (self); GError *error = NULL;
MMPortSerialGps *gps_port;
gps_port = mm_base_modem_peek_port_gps (MM_BASE_MODEM (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)
@@ -4039,13 +4046,13 @@ gps_enabled_ready (MMBaseModem *self,
static void static void
parent_enable_location_gathering_ready (MMIfaceModemLocation *_self, parent_enable_location_gathering_ready (MMIfaceModemLocation *_self,
GAsyncResult *res, GAsyncResult *res,
GTask *task) GTask *task)
{ {
MMBroadbandModemHuawei *self = MM_BROADBAND_MODEM_HUAWEI (_self); MMBroadbandModemHuawei *self = MM_BROADBAND_MODEM_HUAWEI (_self);
LocationGatheringContext *ctx; 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_task_return_error (task, error); g_task_return_error (task, error);
@@ -4070,15 +4077,7 @@ parent_enable_location_gathering_ready (MMIfaceModemLocation *_self,
} }
if (start_gps) { if (start_gps) {
mm_base_modem_at_command_full (MM_BASE_MODEM (self), run_gps_startup (task);
mm_base_modem_peek_port_primary (MM_BASE_MODEM (self)),
gps_startup[ctx->idx],
3,
FALSE,
FALSE, /* raw */
NULL, /* cancellable */
(GAsyncReadyCallback)gps_enabled_ready,
task);
return; return;
} }
@@ -4088,13 +4087,13 @@ parent_enable_location_gathering_ready (MMIfaceModemLocation *_self,
} }
static void static void
enable_location_gathering (MMIfaceModemLocation *self, enable_location_gathering (MMIfaceModemLocation *self,
MMModemLocationSource source, MMModemLocationSource source,
GAsyncReadyCallback callback, GAsyncReadyCallback callback,
gpointer user_data) gpointer user_data)
{ {
LocationGatheringContext *ctx; LocationGatheringContext *ctx;
GTask *task; GTask *task;
ctx = g_new (LocationGatheringContext, 1); ctx = g_new (LocationGatheringContext, 1);
ctx->source = source; ctx->source = source;