- /* We count packets even if the temperature was invalid. This way
- * a sample limit on "Memory" data source still works: unused
- * memory slots come through as "----" measurements. */
- devc->num_samples++;
- if (devc->limit_samples && devc->num_samples >= devc->limit_samples) {
- sdi->driver->dev_acquisition_stop((struct sr_dev_inst *)sdi,
- devc->cb_data);
+ /*
+ * We count packets even if the measurement was invalid. This way
+ * a sample limit on "Memory" data source still works: Unused
+ * memory slots come through as "----" measurements.
+ */
+ devc = sdi->priv;
+ sr_sw_limits_update_samples_read(&devc->limits, 1);
+ if (sr_sw_limits_check(&devc->limits))
+ sr_dev_acquisition_stop(sdi);
+}
+
+static int process_buffer(struct sr_dev_inst *sdi)
+{
+ struct dev_context *devc;
+ uint8_t *pkt;
+ size_t remain, idx;
+
+ /*
+ * Specifically do not insist on finding the packet boundary at
+ * the end of the most recently received data chunk. Serial
+ * ports might involve hardware buffers (FIFO). We want to sync
+ * as fast as possible.
+ *
+ * Handle the synchronized situation first. Process complete
+ * packets that reside at the start of the buffer. Then fallback
+ * to incomplete or unaligned packets if the receive buffer
+ * still contains data bytes. (Depending on the bitrate and the
+ * poll interval, we may always end up in the manual search. But
+ * considering the update rate - two or three packets per second
+ * - this is not an issue.)
+ */
+ devc = sdi->priv;
+ pkt = &devc->packet[0];
+ while (devc->packet_len >= PACKET_SIZE &&
+ pkt[PACKET_SIZE - 2] == SEP[0] &&
+ pkt[PACKET_SIZE - 1] == SEP[1]) {
+ process_packet(sdi, &pkt[0], PACKET_SIZE);
+ remain = devc->packet_len - PACKET_SIZE;
+ if (remain)
+ memmove(&pkt[0], &pkt[PACKET_SIZE], remain);
+ devc->packet_len -= PACKET_SIZE;