]> sigrok.org Git - libsigrok.git/blob - src/soft-trigger.c
soft-trigger: Fix an issue causing triggers to not work.
[libsigrok.git] / src / soft-trigger.c
1 /*
2  * This file is part of the libsigrok project.
3  *
4  * Copyright (C) 2014 Bert Vermeulen <bert@biot.com>
5  *
6  * This program is free software: you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation, either version 3 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
18  */
19
20 #include <config.h>
21 #include <string.h>
22 #include <libsigrok/libsigrok.h>
23 #include "libsigrok-internal.h"
24
25 /* @cond PRIVATE */
26 #define LOG_PREFIX "soft-trigger"
27 /* @endcond */
28
29 SR_PRIV struct soft_trigger_logic *soft_trigger_logic_new(
30                 const struct sr_dev_inst *sdi, struct sr_trigger *trigger,
31                 int pre_trigger_samples)
32 {
33         struct soft_trigger_logic *stl;
34
35         stl = g_malloc0(sizeof(struct soft_trigger_logic));
36         stl->sdi = sdi;
37         stl->trigger = trigger;
38         stl->unitsize = (g_slist_length(sdi->channels) + 7) / 8;
39         stl->prev_sample = g_malloc0(stl->unitsize);
40         stl->pre_trigger_size = stl->unitsize * pre_trigger_samples;
41         stl->pre_trigger_buffer = g_try_malloc(stl->pre_trigger_size);
42         if (pre_trigger_samples > 0 && !stl->pre_trigger_buffer) {
43                 /*
44                  * Error out if g_try_malloc() failed (or was invoked as
45                  * g_try_malloc(0)) *and* more than 0 pretrigger samples
46                  * were requested.
47                  */
48                 soft_trigger_logic_free(stl);
49                 return NULL;
50         }
51         stl->pre_trigger_head = stl->pre_trigger_buffer;
52
53         if (stl->pre_trigger_size > 0 && !stl->pre_trigger_buffer) {
54                 soft_trigger_logic_free(stl);
55                 return NULL;
56         }
57
58         return stl;
59 }
60
61 SR_PRIV void soft_trigger_logic_free(struct soft_trigger_logic *stl)
62 {
63         g_free(stl->pre_trigger_buffer);
64         g_free(stl->prev_sample);
65         g_free(stl);
66 }
67
68 static void pre_trigger_append(struct soft_trigger_logic *stl,
69                 uint8_t *buf, int len)
70 {
71         /* Avoid uselessly copying more than the pre-trigger size. */
72         if (len > stl->pre_trigger_size) {
73                 buf += len - stl->pre_trigger_size;
74                 len = stl->pre_trigger_size;
75         }
76
77         /* Update the filling level of the pre-trigger circular buffer. */
78         stl->pre_trigger_fill = MIN(stl->pre_trigger_fill + len,
79                                     stl->pre_trigger_size);
80
81         /* Actually copy data to the pre-trigger circular buffer. */
82         while (len > 0) {
83                 size_t size = MIN(stl->pre_trigger_buffer + stl->pre_trigger_size
84                                   - stl->pre_trigger_head, len);
85                 memcpy(stl->pre_trigger_head, buf, size);
86                 stl->pre_trigger_head += size;
87                 if (stl->pre_trigger_head >= stl->pre_trigger_buffer
88                                              + stl->pre_trigger_size)
89                         stl->pre_trigger_head = stl->pre_trigger_buffer;
90                 buf += size;
91                 len -= size;
92         }
93 }
94
95 static void pre_trigger_send(struct soft_trigger_logic *stl,
96                 int *pre_trigger_samples)
97 {
98         struct sr_datafeed_packet packet;
99         struct sr_datafeed_logic logic;
100
101         packet.type = SR_DF_LOGIC;
102         packet.payload = &logic;
103         logic.unitsize = stl->unitsize;
104
105         if (pre_trigger_samples)
106                 *pre_trigger_samples = 0;
107
108         /* If pre-trigger buffer not full, rewind head to the first valid sample. */
109         if (stl->pre_trigger_fill < stl->pre_trigger_size)
110                 stl->pre_trigger_head = stl->pre_trigger_buffer;
111
112         /* Send logic packets for the pre-trigger circular buffer content. */
113         while (stl->pre_trigger_fill > 0) {
114                 size_t size = MIN(stl->pre_trigger_buffer + stl->pre_trigger_size
115                                   - stl->pre_trigger_head, stl->pre_trigger_fill);
116                 logic.length = size;
117                 logic.data = stl->pre_trigger_head;
118                 sr_session_send(stl->sdi, &packet);
119                 stl->pre_trigger_head = stl->pre_trigger_buffer;
120                 stl->pre_trigger_fill -= size;
121                 if (pre_trigger_samples)
122                         *pre_trigger_samples += size / stl->unitsize;
123         }
124 }
125
126 static gboolean logic_check_match(struct soft_trigger_logic *stl,
127                 uint8_t *sample, struct sr_trigger_match *match)
128 {
129         int bit, prev_bit;
130         gboolean result;
131
132         stl->count++;
133         result = FALSE;
134         bit = *(sample + match->channel->index / 8)
135                         & (1 << (match->channel->index % 8));
136         if (match->match == SR_TRIGGER_ZERO)
137                 result = bit == 0;
138         else if (match->match == SR_TRIGGER_ONE)
139                 result = bit != 0;
140         else {
141                 /* Edge matches. */
142                 if (stl->count == 1)
143                         /* First sample, don't have enough for an edge match yet. */
144                         return FALSE;
145                 prev_bit = *(stl->prev_sample + match->channel->index / 8)
146                                 & (1 << (match->channel->index % 8));
147                 if (match->match == SR_TRIGGER_RISING)
148                         result = prev_bit == 0 && bit != 0;
149                 else if (match->match == SR_TRIGGER_FALLING)
150                         result = prev_bit != 0 && bit == 0;
151                 else if (match->match == SR_TRIGGER_EDGE)
152                         result = prev_bit != bit;
153         }
154
155         return result;
156 }
157
158 /* Returns the offset (in samples) within buf of where the trigger
159  * occurred, or -1 if not triggered. */
160 SR_PRIV int soft_trigger_logic_check(struct soft_trigger_logic *stl,
161                 uint8_t *buf, int len, int *pre_trigger_samples)
162 {
163         struct sr_datafeed_packet packet;
164         struct sr_trigger_stage *stage;
165         struct sr_trigger_match *match;
166         GSList *l, *l_stage;
167         int offset;
168         int i;
169         gboolean match_found;
170
171         offset = -1;
172         for (i = 0; i < len; i += stl->unitsize) {
173                 l_stage = g_slist_nth(stl->trigger->stages, stl->cur_stage);
174                 stage = l_stage->data;
175                 if (!stage->matches)
176                         /* No matches supplied, client error. */
177                         return SR_ERR_ARG;
178
179                 match_found = TRUE;
180                 for (l = stage->matches; l; l = l->next) {
181                         match = l->data;
182                         if (!match->channel->enabled)
183                                 /* Ignore disabled channels with a trigger. */
184                                 continue;
185                         if (!logic_check_match(stl, buf + i, match)) {
186                                 match_found = FALSE;
187                                 break;
188                         }
189                 }
190                 memcpy(stl->prev_sample, buf + i, stl->unitsize);
191                 if (match_found) {
192                         /* Matched on the current stage. */
193                         if (l_stage->next) {
194                                 /* Advance to next stage. */
195                                 stl->cur_stage++;
196                         } else {
197                                 /* Matched on last stage, send pre-trigger data. */
198                                 pre_trigger_append(stl, buf, i);
199                                 pre_trigger_send(stl, pre_trigger_samples);
200
201                                 /* Fire trigger. */
202                                 offset = i / stl->unitsize;
203
204                                 packet.type = SR_DF_TRIGGER;
205                                 packet.payload = NULL;
206                                 sr_session_send(stl->sdi, &packet);
207                                 break;
208                         }
209                 } else if (stl->cur_stage > 0) {
210                         /*
211                          * We had a match at an earlier stage, but failed on the
212                          * current stage. However, we may have a match on this
213                          * stage in the next bit -- trigger on 0001 will fail on
214                          * seeing 00001, so we need to go back to stage 0 -- but
215                          * at the next sample from the one that matched originally,
216                          * which the counter increment at the end of the loop
217                          * takes care of.
218                          */
219                         i -= stl->cur_stage * stl->unitsize;
220                         if (i < -1)
221                                 i = -1; /* Oops, went back past this buffer. */
222                         /* Reset trigger stage. */
223                         stl->cur_stage = 0;
224                 }
225         }
226
227         if (offset == -1)
228                 pre_trigger_append(stl, buf, len);
229
230         return offset;
231 }