serial-port: stop reading data if the serial port was closed

If a response processor closed the port, don't try to read any
more data from it.  Move the priv->watch_id check to the
while condition so the loop terminates before calling
g_io_channel_read_chars() again, which caused a warning since
the underlying file descriptor was already closed.

Also, bytes_read will never be less than zero (it's unsigned),
so skip the bytes_read > 0 check and just assert that this
condition is true.
This commit is contained in:
Dan Williams
2012-11-26 17:05:52 -06:00
parent a7167b93b9
commit 8772d63389

View File

@@ -771,25 +771,24 @@ data_available (GIOChannel *source,
do { do {
GError *err = NULL; GError *err = NULL;
bytes_read = 0;
status = g_io_channel_read_chars (source, buf, SERIAL_BUF_SIZE, &bytes_read, &err); status = g_io_channel_read_chars (source, buf, SERIAL_BUF_SIZE, &bytes_read, &err);
if (status == G_IO_STATUS_ERROR) { if (status == G_IO_STATUS_ERROR) {
if (err && err->message) if (err && err->message) {
g_warning ("%s", err->message); g_warning ("(%s) read error: %s",
mm_port_get_device (MM_PORT (self)),
err->message);
}
g_clear_error (&err); g_clear_error (&err);
/* Serial port is closed; we're done */
if (priv->watch_id == 0)
break;
} }
/* If no bytes read, just let g_io_channel wait for more data */ /* If no bytes read, just let g_io_channel wait for more data */
if (bytes_read == 0) if (bytes_read == 0)
break; break;
if (bytes_read > 0) { g_assert (bytes_read > 0);
serial_debug (self, "<--", buf, bytes_read); serial_debug (self, "<--", buf, bytes_read);
g_byte_array_append (priv->response, (const guint8 *) buf, bytes_read); g_byte_array_append (priv->response, (const guint8 *) buf, bytes_read);
}
/* Make sure the response doesn't grow too long */ /* Make sure the response doesn't grow too long */
if ((priv->response->len > SERIAL_BUF_SIZE) && priv->spew_control) { if ((priv->response->len > SERIAL_BUF_SIZE) && priv->spew_control) {
@@ -803,7 +802,8 @@ data_available (GIOChannel *source,
priv->n_consecutive_timeouts = 0; priv->n_consecutive_timeouts = 0;
mm_serial_port_got_response (self, err); mm_serial_port_got_response (self, err);
} }
} while (bytes_read == SERIAL_BUF_SIZE || status == G_IO_STATUS_AGAIN); } while ( (bytes_read == SERIAL_BUF_SIZE || status == G_IO_STATUS_AGAIN)
&& (priv->watch_id > 0));
return TRUE; return TRUE;
} }