serial: use timeout API in stream detect, obsoletes bitrate param

The serial_stream_detect() routine needs to estimate the time which is
needed to communicate a given amount of data. Since the serial port got
opened and configured before, the serial communication parameters are
known, and callers need not redundantly specify the bit rate.
This commit is contained in:
Gerhard Sittig 2019-05-01 19:50:01 +02:00
parent d478724801
commit d03815a066
7 changed files with 15 additions and 20 deletions

View File

@ -83,7 +83,7 @@ static GSList *scan(struct sr_dev_driver *di, GSList *options)
/* Let's get a bit of data and see if we can find a packet. */
if (serial_stream_detect(serial, buf, &len, 25,
appa_55ii_packet_valid, 500, 9600) != SR_OK)
appa_55ii_packet_valid, 500) != SR_OK)
goto scan_cleanup;
sr_info("Found device on port %s.", conn);

View File

@ -89,7 +89,7 @@ static GSList *scan(struct sr_dev_driver *di, GSList *options)
/* Let's get a bit of data and see if we can find a packet. */
len = sizeof(buf);
ret = serial_stream_detect(serial, buf, &len, scale->packet_size,
scale->packet_valid, 3000, scale->baudrate);
scale->packet_valid, 3000);
if (ret != SR_OK)
goto scan_cleanup;

View File

@ -106,8 +106,7 @@ static GSList *scan(struct sr_dev_driver *di, GSList *options)
/* Let's get a bit of data and see if we can find a packet. */
len = sizeof(buf);
ret = serial_stream_detect(serial, buf, &len, dmm->packet_size,
dmm->packet_valid, 3000,
dmm->baudrate);
dmm->packet_valid, 3000);
if (ret != SR_OK)
goto scan_cleanup;

View File

@ -79,7 +79,7 @@ static GSList *scan(struct sr_dev_driver *di, GSList *options)
/* Let's get a bit of data and see if we can find a packet. */
if (serial_stream_detect(serial, buf, &len, len,
teleinfo_packet_valid, 3000, 1200) != SR_OK)
teleinfo_packet_valid, 3000) != SR_OK)
goto scan_cleanup;
sr_info("Found device on port %s.", conn);

View File

@ -209,7 +209,7 @@ static int serial_stream_check_buf(struct sr_serial_dev_inst *serial,
uint8_t *buf, size_t buflen,
size_t packet_size,
packet_valid_callback is_valid,
uint64_t timeout_ms, int baudrate)
uint64_t timeout_ms)
{
size_t len, dropped;
int ret;
@ -221,7 +221,7 @@ static int serial_stream_check_buf(struct sr_serial_dev_inst *serial,
len = buflen;
ret = serial_stream_detect(serial, buf, &len, packet_size,
is_valid, timeout_ms, baudrate);
is_valid, timeout_ms);
serial_close(serial);
@ -245,12 +245,12 @@ static int serial_stream_check_buf(struct sr_serial_dev_inst *serial,
static int serial_stream_check(struct sr_serial_dev_inst *serial,
size_t packet_size,
packet_valid_callback is_valid,
uint64_t timeout_ms, int baudrate)
uint64_t timeout_ms)
{
uint8_t buf[128];
return serial_stream_check_buf(serial, buf, sizeof(buf), packet_size,
is_valid, timeout_ms, baudrate);
is_valid, timeout_ms);
}
/*
@ -713,8 +713,7 @@ SR_PRIV struct sr_dev_inst *es51919_serial_scan(GSList *options,
if (!(serial = serial_dev_new(options, "9600/8n1/rts=1/dtr=1")))
goto scan_cleanup;
ret = serial_stream_check(serial, PACKET_SIZE, packet_valid,
3000, 9600);
ret = serial_stream_check(serial, PACKET_SIZE, packet_valid, 3000);
if (ret != SR_OK)
goto scan_cleanup;

View File

@ -1175,7 +1175,7 @@ SR_PRIV int serial_stream_detect(struct sr_serial_dev_inst *serial,
uint8_t *buf, size_t *buflen,
size_t packet_size,
packet_valid_callback is_valid,
uint64_t timeout_ms, int baudrate);
uint64_t timeout_ms);
SR_PRIV int sr_serial_extract_options(GSList *options, const char **serial_device,
const char **serial_options);
SR_PRIV int serial_source_add(struct sr_session *session,

View File

@ -749,9 +749,6 @@ SR_PRIV int serial_readline(struct sr_serial_dev_inst *serial,
* @param is_valid Callback that assesses whether the packet is valid or not.
* @param[in] timeout_ms The timeout after which, if no packet is detected, to
* abort scanning.
* @param[in] baudrate The baudrate of the serial port. This parameter is not
* critical, but it helps fine tune the serial port polling
* delay.
*
* @retval SR_OK Valid packet was found within the given timeout.
* @retval SR_ERR Failure.
@ -762,7 +759,7 @@ SR_PRIV int serial_stream_detect(struct sr_serial_dev_inst *serial,
uint8_t *buf, size_t *buflen,
size_t packet_size,
packet_valid_callback is_valid,
uint64_t timeout_ms, int baudrate)
uint64_t timeout_ms)
{
uint64_t start, time, byte_delay_us;
size_t ibuf, i, maxlen;
@ -770,16 +767,16 @@ SR_PRIV int serial_stream_detect(struct sr_serial_dev_inst *serial,
maxlen = *buflen;
sr_dbg("Detecting packets on %s (timeout = %" PRIu64
"ms, baudrate = %d).", serial->port, timeout_ms, baudrate);
sr_dbg("Detecting packets on %s (timeout = %" PRIu64 "ms).",
serial->port, timeout_ms);
if (maxlen < (packet_size / 2) ) {
if (maxlen < (packet_size * 2) ) {
sr_err("Buffer size must be at least twice the packet size.");
return SR_ERR;
}
/* Assume 8n1 transmission. That is 10 bits for every byte. */
byte_delay_us = 10 * ((1000 * 1000) / baudrate);
byte_delay_us = serial_timeout(serial, 1) * 1000;
start = g_get_monotonic_time();
i = ibuf = len = 0;