+static void send_block_to_session_bus(struct la8 *la8, int block)
+{
+ int i;
+ uint8_t sample, expected_sample;
+ struct sr_datafeed_packet packet;
+ struct sr_datafeed_logic logic;
+ 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 < BS; i++) {
+ /* Don't continue if the trigger was found previously. */
+ if (la8->trigger_found)
+ break;
+
+ /*
+ * Also, don't continue if triggers are "don't care", i.e. if
+ * no trigger conditions were specified by the user. In that
+ * case we don't want to send an SR_DF_TRIGGER packet at all.
+ */
+ if (la8->trigger_mask == 0x00)
+ break;
+
+ sample = *(la8->final_buf + (block * BS) + 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 an SR_DF_LOGIC packet to the session bus. */
+ sr_spew("la8: sending SR_DF_LOGIC packet (%d bytes) for "
+ "block %d", BS, block);
+ packet.type = SR_DF_LOGIC;
+ packet.timeoffset = block * BS * la8->period_ps;
+ packet.duration = BS * la8->period_ps;
+ packet.payload = &logic;
+ logic.length = BS;
+ logic.unitsize = 1;
+ logic.data = la8->final_buf + (block * BS);
+ 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_spew("la8: sending pre-trigger SR_DF_LOGIC packet, "
+ "start = %d, length = %d", block * BS, trigger_point);
+ packet.type = SR_DF_LOGIC;
+ packet.timeoffset = block * BS * la8->period_ps;
+ packet.duration = trigger_point * la8->period_ps;
+ packet.payload = &logic;
+ logic.length = trigger_point;
+ logic.unitsize = 1;
+ logic.data = la8->final_buf + (block * BS);
+ sr_session_bus(la8->session_id, &packet);
+ }
+
+ /* Send the SR_DF_TRIGGER packet to the session bus. */
+ sr_spew("la8: sending SR_DF_TRIGGER packet, sample = %d",
+ (block * BS) + trigger_point);
+ packet.type = SR_DF_TRIGGER;
+ packet.timeoffset = (block * BS + trigger_point) * la8->period_ps;
+ packet.duration = 0;
+ packet.payload = NULL;
+ sr_session_bus(la8->session_id, &packet);
+
+ /* If at least one sample is located after the trigger... */
+ if (trigger_point < (BS - 1)) {
+ /* Send post-trigger SR_DF_LOGIC packet to the session bus. */
+ sr_spew("la8: sending post-trigger SR_DF_LOGIC packet, "
+ "start = %d, length = %d",
+ (block * BS) + trigger_point, BS - trigger_point);
+ packet.type = SR_DF_LOGIC;
+ packet.timeoffset = (block * BS + trigger_point) * la8->period_ps;
+ packet.duration = (BS - trigger_point) * la8->period_ps;
+ packet.payload = &logic;
+ logic.length = BS - trigger_point;
+ logic.unitsize = 1;
+ logic.data = la8->final_buf + (block * BS) + trigger_point;
+ sr_session_bus(la8->session_id, &packet);
+ }
+}
+
+static int receive_data(int fd, int revents, void *session_data)