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