From: Gerhard Sittig Date: Wed, 1 May 2019 17:50:01 +0000 (+0200) Subject: serial: use timeout API in stream detect, obsoletes bitrate param X-Git-Url: http://sigrok.org/gitweb/?a=commitdiff_plain;h=d03815a0662a905392e428612507ad4de9f2f8b1;p=libsigrok.git 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. --- diff --git a/src/hardware/appa-55ii/api.c b/src/hardware/appa-55ii/api.c index ddf3fe4a..81eafdce 100644 --- a/src/hardware/appa-55ii/api.c +++ b/src/hardware/appa-55ii/api.c @@ -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); diff --git a/src/hardware/kern-scale/api.c b/src/hardware/kern-scale/api.c index e369a44c..fba3e4a5 100644 --- a/src/hardware/kern-scale/api.c +++ b/src/hardware/kern-scale/api.c @@ -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; diff --git a/src/hardware/serial-dmm/api.c b/src/hardware/serial-dmm/api.c index 009f37c5..f237bb2a 100644 --- a/src/hardware/serial-dmm/api.c +++ b/src/hardware/serial-dmm/api.c @@ -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; diff --git a/src/hardware/teleinfo/api.c b/src/hardware/teleinfo/api.c index 71a14106..3de4456b 100644 --- a/src/hardware/teleinfo/api.c +++ b/src/hardware/teleinfo/api.c @@ -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); diff --git a/src/lcr/es51919.c b/src/lcr/es51919.c index 38750098..fad0a3e9 100644 --- a/src/lcr/es51919.c +++ b/src/lcr/es51919.c @@ -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; diff --git a/src/libsigrok-internal.h b/src/libsigrok-internal.h index 9a34ba16..5a9d3951 100644 --- a/src/libsigrok-internal.h +++ b/src/libsigrok-internal.h @@ -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, diff --git a/src/serial.c b/src/serial.c index 12026a89..a2d0189f 100644 --- a/src/serial.c +++ b/src/serial.c @@ -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;