+static void send_block_to_session_bus(struct la8 *la8, int block)
+{
+ int i;
+ uint8_t sample, expected_sample;
+ struct sr_datafeed_packet packet;
+ int trigger_point; /* Relative trigger point (in this block). */
+
+ /* Note: No sanity checks on la8/block, caller is responsible. */
+
+ /* Check if we can find the trigger condition in this block. */
+ trigger_point = -1;
+ expected_sample = la8->trigger_pattern & la8->trigger_mask;
+ for (i = 0; i < 4096; i++) {
+ /* Don't continue if the trigger was found previously. */
+ if (la8->trigger_found)
+ break;
+
+ sample = *(la8->final_buf + (block * 4096) + i);
+
+ if ((sample & la8->trigger_mask) == expected_sample) {
+ trigger_point = i;
+ la8->trigger_found = 1;
+ break;
+ }
+ }
+
+ /* If no trigger was found, send one SR_DF_LOGIC packet. */
+ if (trigger_point == -1) {
+ /* Send a 4096 byte SR_DF_LOGIC packet to the session bus. */
+ // sr_dbg("la8: %s: sending SR_DF_LOGIC packet", __func__);
+ packet.type = SR_DF_LOGIC;
+ packet.length = 4096;
+ packet.unitsize = 1;
+ packet.payload = la8->final_buf + (block * 4096);
+ sr_session_bus(la8->session_id, &packet);
+ return;
+ }
+
+ /*
+ * We found the trigger, so some special handling is needed. We have
+ * to send an SR_DF_LOGIC packet with the samples before the trigger
+ * (if any), then the SD_DF_TRIGGER packet itself, then another
+ * SR_DF_LOGIC packet with the samples after the trigger (if any).
+ */
+
+ /* TODO: Send SR_DF_TRIGGER packet before or after the actual sample? */
+
+ /* If at least one sample is located before the trigger... */
+ if (trigger_point > 0) {
+ /* Send pre-trigger SR_DF_LOGIC packet to the session bus. */
+ sr_dbg("la8: %s: sending pre-trigger SR_DF_LOGIC packet, ",
+ "start = %" PRIu64 ", length = %d", __func__,
+ block * 4096, trigger_point);
+ packet.type = SR_DF_LOGIC;
+ packet.length = trigger_point;
+ packet.unitsize = 1;
+ packet.payload = la8->final_buf + (block * 4096);
+ sr_session_bus(la8->session_id, &packet);
+ }
+
+ /* Send the SR_DF_TRIGGER packet to the session bus. */
+ sr_dbg("la8: %s: sending SR_DF_TRIGGER packet, sample = %" PRIu64,
+ __func__, (block * 4096) + trigger_point);
+ packet.type = SR_DF_TRIGGER;
+ packet.length = 0;
+ packet.unitsize = 0;
+ packet.payload = NULL;
+ sr_session_bus(la8->session_id, &packet);
+
+ /* If at least one sample is located after the trigger... */
+ if (trigger_point < (4096 - 1)) {
+ /* Send post-trigger SR_DF_LOGIC packet to the session bus. */
+ sr_dbg("la8: %s: sending post-trigger SR_DF_LOGIC packet, ",
+ "start = %" PRIu64 ", length = %d", __func__,
+ (block * 4096) + trigger_point,
+ (4096 - 1) - trigger_point);
+ packet.type = SR_DF_LOGIC;
+ packet.length = (4096 - 1) - trigger_point;
+ packet.unitsize = 1;
+ packet.payload = la8->final_buf + (block * 4096)
+ + trigger_point;
+ sr_session_bus(la8->session_id, &packet);
+ }
+}
+