+/**
+ * Read a line from the specified serial port.
+ *
+ * @param serial Previously initialized serial port structure.
+ * @param buf Buffer where to store the bytes that are read.
+ * @param buflen Size of the buffer.
+ * @param timeout_ms How long to wait for a line to come in.
+ *
+ * Reading stops when CR of LR is found, which is stripped from the buffer.
+ *
+ * @return SR_OK on success, SR_ERR on failure.
+ */
+SR_PRIV int serial_readline(struct sr_serial_dev_inst *serial, char **buf,
+ int *buflen, gint64 timeout_ms)
+{
+ gint64 start;
+ int maxlen, len;
+
+ if (!serial || serial->fd == -1) {
+ sr_dbg("Invalid serial port.");
+ return SR_ERR;
+ }
+
+ if (serial->fd == -1) {
+ sr_dbg("Cannot use unopened serial port %s (fd %d).",
+ serial->port, serial->fd);
+ return -1;
+ }
+
+ timeout_ms *= 1000;
+ start = g_get_monotonic_time();
+
+ maxlen = *buflen;
+ *buflen = len = 0;
+ while(1) {
+ len = maxlen - *buflen - 1;
+ if (len < 1)
+ break;
+ len = serial_read(serial, *buf + *buflen, 1);
+ if (len > 0) {
+ *buflen += len;
+ *(*buf + *buflen) = '\0';
+ if (*buflen > 0 && (*(*buf + *buflen - 1) == '\r'
+ || *(*buf + *buflen - 1) == '\n')) {
+ /* Strip CR/LF and terminate. */
+ *(*buf + --*buflen) = '\0';
+ break;
+ }
+ }
+ if (g_get_monotonic_time() - start > timeout_ms)
+ /* Timeout */
+ break;
+ g_usleep(2000);
+ }
+ if (*buflen)
+ sr_dbg("Received %d: '%s'.", *buflen, *buf);
+
+ return SR_OK;
+}
+
+/**
+ * Try to find a valid packet in a serial data stream.
+ *
+ * @param serial Previously initialized serial port structure.
+ * @param buf Buffer containing the bytes to write.
+ * @param count Size of the buffer.
+ * @param packet_size Size, in bytes, of a valid packet.
+ * @param is_valid Callback that assesses whether the packet is valid or not.
+ * @param timeout_ms The timeout after which, if no packet is detected, to
+ * abort scanning.
+ * @param baudrate The baudrate of the serial port. This parameter is not
+ * critical, but it helps fine tune the serial port polling
+ * delay.
+ *
+ * @return SR_OK if a valid packet is found within the given timeout,
+ * SR_ERR upon failure.
+ */
+SR_PRIV int serial_stream_detect(struct sr_serial_dev_inst *serial,
+ uint8_t *buf, size_t *buflen,
+ size_t packet_size, packet_valid_t is_valid,
+ uint64_t timeout_ms, int baudrate)
+{
+ uint64_t start, time, byte_delay_us;
+ size_t ibuf, i, maxlen;
+ int len;
+
+ maxlen = *buflen;
+
+ sr_dbg("Detecting packets on FD %d (timeout = %" PRIu64
+ "ms, baudrate = %d).", serial->fd, timeout_ms, baudrate);
+
+ 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 * (1000000 / baudrate);
+ start = g_get_monotonic_time();
+
+ i = ibuf = len = 0;
+ while (ibuf < maxlen) {
+ len = serial_read(serial, &buf[ibuf], 1);
+ if (len > 0) {
+ ibuf += len;
+ } else if (len == 0) {
+ sr_spew("Error: Only read 0 bytes.");
+ } else {
+ /* Error reading byte, but continuing anyway. */
+ }
+
+ time = g_get_monotonic_time() - start;
+ time /= 1000;
+
+ if ((ibuf - i) >= packet_size) {
+ /* We have at least a packet's worth of data. */
+ if (is_valid(&buf[i])) {
+ sr_spew("Found valid %d-byte packet after "
+ "%" PRIu64 "ms.", (ibuf - i), time);
+ *buflen = ibuf;
+ return SR_OK;
+ } else {
+ sr_spew("Got %d bytes, but not a valid "
+ "packet.", (ibuf - i));
+ }
+ /* Not a valid packet. Continue searching. */
+ i++;
+ }
+ if (time >= timeout_ms) {
+ /* Timeout */
+ sr_dbg("Detection timed out after %dms.", time);
+ break;
+ }
+ g_usleep(byte_delay_us);
+ }
+
+ *buflen = ibuf;
+
+ sr_err("Didn't find a valid packet (read %d bytes).", *buflen);
+
+ return SR_ERR;
+}