]> sigrok.org Git - libsigrok.git/blob - src/soft-trigger.c
soft-trigger: Use g_try_malloc() for potentially large allocation.
[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 (!stl->pre_trigger_buffer) {
43                 soft_trigger_logic_free(stl);
44                 return NULL;
45         }
46         stl->pre_trigger_head = stl->pre_trigger_buffer;
47
48         if (stl->pre_trigger_size > 0 && !stl->pre_trigger_buffer) {
49                 soft_trigger_logic_free(stl);
50                 return NULL;
51         }
52
53         return stl;
54 }
55
56 SR_PRIV void soft_trigger_logic_free(struct soft_trigger_logic *stl)
57 {
58         g_free(stl->pre_trigger_buffer);
59         g_free(stl->prev_sample);
60         g_free(stl);
61 }
62
63 static void pre_trigger_append(struct soft_trigger_logic *stl,
64                 uint8_t *buf, int len)
65 {
66         /* Avoid uselessly copying more than the pre-trigger size. */
67         if (len > stl->pre_trigger_size) {
68                 buf += len - stl->pre_trigger_size;
69                 len = stl->pre_trigger_size;
70         }
71
72         /* Update the filling level of the pre-trigger circular buffer. */
73         stl->pre_trigger_fill = MIN(stl->pre_trigger_fill + len,
74                                     stl->pre_trigger_size);
75
76         /* Actually copy data to the pre-trigger circular buffer. */
77         while (len > 0) {
78                 size_t size = MIN(stl->pre_trigger_buffer + stl->pre_trigger_size
79                                   - stl->pre_trigger_head, len);
80                 memcpy(stl->pre_trigger_head, buf, size);
81                 stl->pre_trigger_head += size;
82                 if (stl->pre_trigger_head >= stl->pre_trigger_buffer
83                                              + stl->pre_trigger_size)
84                         stl->pre_trigger_head = stl->pre_trigger_buffer;
85                 buf += size;
86                 len -= size;
87         }
88 }
89
90 static void pre_trigger_send(struct soft_trigger_logic *stl,
91                 int *pre_trigger_samples)
92 {
93         struct sr_datafeed_packet packet;
94         struct sr_datafeed_logic logic;
95
96         packet.type = SR_DF_LOGIC;
97         packet.payload = &logic;
98         logic.unitsize = stl->unitsize;
99
100         if (pre_trigger_samples)
101                 *pre_trigger_samples = 0;
102
103         /* If pre-trigger buffer not full, rewind head to the first valid sample. */
104         if (stl->pre_trigger_fill < stl->pre_trigger_size)
105                 stl->pre_trigger_head = stl->pre_trigger_buffer;
106
107         /* Send logic packets for the pre-trigger circular buffer content. */
108         while (stl->pre_trigger_fill > 0) {
109                 size_t size = MIN(stl->pre_trigger_buffer + stl->pre_trigger_size
110                                   - stl->pre_trigger_head, stl->pre_trigger_fill);
111                 logic.length = size;
112                 logic.data = stl->pre_trigger_head;
113                 sr_session_send(stl->sdi, &packet);
114                 stl->pre_trigger_head = stl->pre_trigger_buffer;
115                 stl->pre_trigger_fill -= size;
116                 if (pre_trigger_samples)
117                         *pre_trigger_samples += size / stl->unitsize;
118         }
119 }
120
121 static gboolean logic_check_match(struct soft_trigger_logic *stl,
122                 uint8_t *sample, struct sr_trigger_match *match)
123 {
124         int bit, prev_bit;
125         gboolean result;
126
127         stl->count++;
128         result = FALSE;
129         bit = *(sample + match->channel->index / 8)
130                         & (1 << (match->channel->index % 8));
131         if (match->match == SR_TRIGGER_ZERO)
132                 result = bit == 0;
133         else if (match->match == SR_TRIGGER_ONE)
134                 result = bit != 0;
135         else {
136                 /* Edge matches. */
137                 if (stl->count == 1)
138                         /* First sample, don't have enough for an edge match yet. */
139                         return FALSE;
140                 prev_bit = *(stl->prev_sample + match->channel->index / 8)
141                                 & (1 << (match->channel->index % 8));
142                 if (match->match == SR_TRIGGER_RISING)
143                         result = prev_bit == 0 && bit != 0;
144                 else if (match->match == SR_TRIGGER_FALLING)
145                         result = prev_bit != 0 && bit == 0;
146                 else if (match->match == SR_TRIGGER_EDGE)
147                         result = prev_bit != bit;
148         }
149
150         return result;
151 }
152
153 /* Returns the offset (in samples) within buf of where the trigger
154  * occurred, or -1 if not triggered. */
155 SR_PRIV int soft_trigger_logic_check(struct soft_trigger_logic *stl,
156                 uint8_t *buf, int len, int *pre_trigger_samples)
157 {
158         struct sr_datafeed_packet packet;
159         struct sr_trigger_stage *stage;
160         struct sr_trigger_match *match;
161         GSList *l, *l_stage;
162         int offset;
163         int i;
164         gboolean match_found;
165
166         offset = -1;
167         for (i = 0; i < len; i += stl->unitsize) {
168                 l_stage = g_slist_nth(stl->trigger->stages, stl->cur_stage);
169                 stage = l_stage->data;
170                 if (!stage->matches)
171                         /* No matches supplied, client error. */
172                         return SR_ERR_ARG;
173
174                 match_found = TRUE;
175                 for (l = stage->matches; l; l = l->next) {
176                         match = l->data;
177                         if (!match->channel->enabled)
178                                 /* Ignore disabled channels with a trigger. */
179                                 continue;
180                         if (!logic_check_match(stl, buf + i, match)) {
181                                 match_found = FALSE;
182                                 break;
183                         }
184                 }
185                 memcpy(stl->prev_sample, buf + i, stl->unitsize);
186                 if (match_found) {
187                         /* Matched on the current stage. */
188                         if (l_stage->next) {
189                                 /* Advance to next stage. */
190                                 stl->cur_stage++;
191                         } else {
192                                 /* Matched on last stage, send pre-trigger data. */
193                                 pre_trigger_append(stl, buf, i);
194                                 pre_trigger_send(stl, pre_trigger_samples);
195
196                                 /* Fire trigger. */
197                                 offset = i / stl->unitsize;
198
199                                 packet.type = SR_DF_TRIGGER;
200                                 packet.payload = NULL;
201                                 sr_session_send(stl->sdi, &packet);
202                                 break;
203                         }
204                 } else if (stl->cur_stage > 0) {
205                         /*
206                          * We had a match at an earlier stage, but failed on the
207                          * current stage. However, we may have a match on this
208                          * stage in the next bit -- trigger on 0001 will fail on
209                          * seeing 00001, so we need to go back to stage 0 -- but
210                          * at the next sample from the one that matched originally,
211                          * which the counter increment at the end of the loop
212                          * takes care of.
213                          */
214                         i -= stl->cur_stage * stl->unitsize;
215                         if (i < -1)
216                                 i = -1; /* Oops, went back past this buffer. */
217                         /* Reset trigger stage. */
218                         stl->cur_stage = 0;
219                 }
220         }
221
222         if (offset == -1)
223                 pre_trigger_append(stl, buf, len);
224
225         return offset;
226 }