port-serial: allow non-tty 'MMPortSerial' objects

Allow having 'MMPortSerial' objects for non-tty devices. This will allow us e.g.
handling /dev/cdc-wdm ports speaking the AT protocol.
This commit is contained in:
Aleksander Morgado
2013-11-19 20:22:45 +01:00
parent 6fc14f6611
commit 3ef71e4541

View File

@@ -177,7 +177,7 @@ mm_port_serial_command (MMPortSerial *self,
ctx->cancellable = (cancellable ? g_object_ref (cancellable) : NULL);
/* Only accept about 3 seconds of EAGAIN for this command */
if (self->priv->send_delay)
if (self->priv->send_delay && mm_port_get_subsys (MM_PORT (self)) == MM_PORT_SUBSYS_TTY)
ctx->eagain_count = 3000000 / self->priv->send_delay;
else
ctx->eagain_count = 1000;
@@ -429,6 +429,10 @@ real_config_fd (MMPortSerial *self, int fd, GError **error)
int parity;
int stopbits;
/* No setup if not a tty */
if (mm_port_get_subsys (MM_PORT (self)) != MM_PORT_SUBSYS_TTY)
return TRUE;
speed = parse_baudrate (self->priv->baud);
bits = parse_bits (self->priv->bits);
parity = parse_parity (self->priv->parity);
@@ -541,7 +545,7 @@ port_serial_process_command (MMPortSerial *self,
serial_debug (self, "-->", (const char *) ctx->command->data, ctx->command->len);
}
if (self->priv->send_delay == 0) {
if (self->priv->send_delay == 0 || mm_port_get_subsys (MM_PORT (self)) != MM_PORT_SUBSYS_TTY) {
/* Send the whole command in one write */
send_len = expected_status = ctx->command->len;
p = ctx->command->data;
@@ -763,7 +767,10 @@ port_serial_queue_process (gpointer data)
/* Schedule the next byte of the command to be sent */
if (!ctx->done) {
port_serial_schedule_queue_process (self, self->priv->send_delay / 1000);
port_serial_schedule_queue_process (self,
(mm_port_get_subsys (MM_PORT (self)) == MM_PORT_SUBSYS_TTY ?
self->priv->send_delay / 1000 :
0));
return FALSE;
}
@@ -992,6 +999,9 @@ mm_port_serial_open (MMPortSerial *self, GError **error)
return FALSE;
}
/* Serial port specific setup */
if (mm_port_get_subsys (MM_PORT (self)) == MM_PORT_SUBSYS_TTY) {
/* Try to lock serial device */
if (ioctl (self->priv->fd, TIOCEXCL) < 0) {
errno_save = errno;
g_set_error (error, MM_SERIAL_ERROR, MM_SERIAL_ERROR_OPEN_FAILED,
@@ -1011,12 +1021,6 @@ mm_port_serial_open (MMPortSerial *self, GError **error)
goto error;
}
g_warn_if_fail (MM_PORT_SERIAL_GET_CLASS (self)->config_fd);
if (!MM_PORT_SERIAL_GET_CLASS (self)->config_fd (self, self->priv->fd, error)) {
mm_dbg ("(%s) failed to configure serial device", device);
goto error;
}
/* Don't wait for pending data when closing the port; this can cause some
* stupid devices that don't respond to URBs on a particular port to hang
* for 30 seconds when probing fails. See GNOME bug #630670.
@@ -1027,6 +1031,13 @@ mm_port_serial_open (MMPortSerial *self, GError **error)
mm_warn ("(%s): couldn't set serial port closing_wait to none: %s",
device, g_strerror (errno));
}
}
g_warn_if_fail (MM_PORT_SERIAL_GET_CLASS (self)->config_fd);
if (!MM_PORT_SERIAL_GET_CLASS (self)->config_fd (self, self->priv->fd, error)) {
mm_dbg ("(%s) failed to configure serial device", device);
goto error;
}
g_get_current_time (&tv_end);
@@ -1108,6 +1119,18 @@ mm_port_serial_close (MMPortSerial *self)
mm_port_set_connected (MM_PORT (self), FALSE);
/* Destroy channel */
if (self->priv->channel) {
data_watch_enable (self, FALSE);
g_io_channel_shutdown (self->priv->channel, TRUE, NULL);
g_io_channel_unref (self->priv->channel);
self->priv->channel = NULL;
}
g_get_current_time (&tv_start);
/* Serial port specific setup */
if (mm_port_get_subsys (MM_PORT (self)) == MM_PORT_SUBSYS_TTY) {
/* Paranoid: ensure our closing_wait value is still set so we ignore
* pending data when closing the port. See GNOME bug #630670.
*/
@@ -1121,17 +1144,10 @@ mm_port_serial_close (MMPortSerial *self)
}
}
g_get_current_time (&tv_start);
if (self->priv->channel) {
data_watch_enable (self, FALSE);
g_io_channel_shutdown (self->priv->channel, TRUE, NULL);
g_io_channel_unref (self->priv->channel);
self->priv->channel = NULL;
}
tcsetattr (self->priv->fd, TCSANOW, &self->priv->old_t);
tcflush (self->priv->fd, TCIOFLUSH);
}
close (self->priv->fd);
self->priv->fd = -1;
@@ -1485,7 +1501,7 @@ flash_do (MMPortSerial *self)
ctx->flash_id = 0;
if (self->priv->flash_ok) {
if (self->priv->flash_ok || mm_port_get_subsys (MM_PORT (self)) == MM_PORT_SUBSYS_TTY) {
if (ctx->current_speed) {
if (!set_speed (ctx->self, ctx->current_speed, &error))
g_assert (error);
@@ -1544,7 +1560,7 @@ mm_port_serial_flash (MMPortSerial *self,
return;
}
if (!self->priv->flash_ok) {
if (!self->priv->flash_ok || mm_port_get_subsys (MM_PORT (self)) != MM_PORT_SUBSYS_TTY) {
self->priv->flash_ctx = ctx;
ctx->flash_id = g_idle_add ((GSourceFunc)flash_do, self);
return;