]>
Commit | Line | Data |
---|---|---|
6239c175 UH |
1 | /* |
2 | * This file is part of the sigrok project. | |
3 | * | |
4 | * Copyright (C) 2010 Uwe Hermann <uwe@hermann-uwe.de> | |
fc96e6f8 | 5 | * Copyright (C) 2011 Olivier Fauchon <olivier@aixmarseille.com> |
6239c175 UH |
6 | * |
7 | * This program is free software; you can redistribute it and/or modify | |
8 | * it under the terms of the GNU General Public License as published by | |
9 | * the Free Software Foundation; either version 2 of the License, or | |
10 | * (at your option) any later version. | |
11 | * | |
12 | * This program is distributed in the hope that it will be useful, | |
13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
15 | * GNU General Public License for more details. | |
16 | * | |
17 | * You should have received a copy of the GNU General Public License | |
18 | * along with this program; if not, write to the Free Software | |
19 | * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA | |
20 | */ | |
21 | ||
22 | #include <stdlib.h> | |
85b5af06 | 23 | #include <unistd.h> |
6239c175 UH |
24 | #include <string.h> |
25 | #include <sigrok.h> | |
26 | #include "config.h" | |
27 | ||
e15f48c2 BV |
28 | #define NUM_PROBES 8 |
29 | #define DEMONAME "Demo device" | |
30 | /* size of chunks to send through the session bus */ | |
31 | #define BUFSIZE 4096 | |
85b5af06 | 32 | |
6239c175 | 33 | |
e15f48c2 BV |
34 | enum { |
35 | GENMODE_RANDOM, | |
36 | GENMODE_INC, | |
37 | }; | |
85b5af06 | 38 | |
e15f48c2 BV |
39 | struct databag { |
40 | int pipe_fds[2]; | |
41 | uint8_t sample_generator; | |
42 | uint8_t thread_running; | |
43 | uint64_t samples_counter; | |
44 | int device_index; | |
45 | int loop_sleep; | |
46 | gpointer session_device_id; | |
47 | }; | |
85b5af06 UH |
48 | |
49 | static GThread *my_thread; | |
50 | static int thread_running; | |
51 | ||
6239c175 UH |
52 | static int capabilities[] = { |
53 | HWCAP_LOGIC_ANALYZER, | |
e15f48c2 | 54 | HWCAP_PATTERN_MODE, |
6239c175 | 55 | HWCAP_LIMIT_SAMPLES, |
ba3d481b | 56 | HWCAP_CONTINUOUS |
6239c175 UH |
57 | }; |
58 | ||
e15f48c2 BV |
59 | static char *patternmodes[] = { |
60 | "random", | |
61 | "incremental", | |
62 | NULL | |
85b5af06 UH |
63 | }; |
64 | ||
65 | /* List of struct sigrok_device_instance, maintained by opendev()/closedev(). */ | |
66 | static GSList *device_instances = NULL; | |
6239c175 | 67 | static uint64_t cur_samplerate = 0; |
ba3d481b | 68 | static uint64_t limit_samples = -1; |
e15f48c2 | 69 | static int default_genmode = GENMODE_RANDOM; |
6239c175 | 70 | |
6239c175 | 71 | |
6239c175 UH |
72 | static void hw_stop_acquisition(int device_index, gpointer session_device_id); |
73 | ||
6239c175 UH |
74 | static int hw_init(char *deviceinfo) |
75 | { | |
85b5af06 UH |
76 | struct sigrok_device_instance *sdi; |
77 | ||
e15f48c2 BV |
78 | /* Avoid compiler warnings. */ |
79 | deviceinfo = deviceinfo; | |
85b5af06 | 80 | |
e15f48c2 | 81 | sdi = sigrok_device_instance_new(0, ST_ACTIVE, DEMONAME, NULL, NULL); |
85b5af06 UH |
82 | if (!sdi) |
83 | return 0; | |
e15f48c2 | 84 | |
85b5af06 UH |
85 | device_instances = g_slist_append(device_instances, sdi); |
86 | ||
87 | return 1; | |
6239c175 UH |
88 | } |
89 | ||
90 | static int hw_opendev(int device_index) | |
91 | { | |
17e1afcb | 92 | /* Avoid compiler warnings. */ |
6239c175 UH |
93 | device_index = device_index; |
94 | ||
95 | /* Nothing needed so far. */ | |
96 | return SIGROK_OK; | |
97 | } | |
98 | ||
99 | static void hw_closedev(int device_index) | |
100 | { | |
17e1afcb | 101 | /* Avoid compiler warnings. */ |
6239c175 UH |
102 | device_index = device_index; |
103 | ||
104 | /* Nothing needed so far. */ | |
105 | } | |
106 | ||
107 | static void hw_cleanup(void) | |
108 | { | |
109 | /* Nothing needed so far. */ | |
110 | } | |
111 | ||
112 | static void *hw_get_device_info(int device_index, int device_info_id) | |
113 | { | |
85b5af06 UH |
114 | struct sigrok_device_instance *sdi; |
115 | void *info = NULL; | |
116 | ||
117 | if (!(sdi = get_sigrok_device_instance(device_instances, device_index))) | |
118 | return NULL; | |
119 | ||
6239c175 UH |
120 | switch (device_info_id) { |
121 | case DI_INSTANCE: | |
85b5af06 | 122 | info = sdi; |
6239c175 UH |
123 | break; |
124 | case DI_NUM_PROBES: | |
125 | info = GINT_TO_POINTER(NUM_PROBES); | |
126 | break; | |
6239c175 UH |
127 | case DI_CUR_SAMPLERATE: |
128 | info = &cur_samplerate; | |
129 | break; | |
e15f48c2 BV |
130 | case DI_PATTERNMODES: |
131 | info = &patternmodes; | |
132 | break; | |
6239c175 UH |
133 | } |
134 | ||
135 | return info; | |
136 | } | |
137 | ||
138 | static int hw_get_status(int device_index) | |
139 | { | |
17e1afcb | 140 | /* Avoid compiler warnings. */ |
6239c175 | 141 | device_index = device_index; |
5096c6a6 | 142 | |
e15f48c2 | 143 | return ST_ACTIVE; |
6239c175 UH |
144 | } |
145 | ||
146 | static int *hw_get_capabilities(void) | |
147 | { | |
e15f48c2 | 148 | |
6239c175 UH |
149 | return capabilities; |
150 | } | |
151 | ||
152 | static int hw_set_configuration(int device_index, int capability, void *value) | |
153 | { | |
154 | int ret; | |
85b5af06 | 155 | uint64_t *tmp_u64; |
e15f48c2 | 156 | char *stropt; |
6239c175 | 157 | |
17e1afcb | 158 | /* Avoid compiler warnings. */ |
6239c175 UH |
159 | device_index = device_index; |
160 | ||
e15f48c2 BV |
161 | if (capability == HWCAP_PROBECONFIG) { |
162 | /* nothing to do */ | |
85b5af06 | 163 | ret = SIGROK_OK; |
6239c175 | 164 | } else if (capability == HWCAP_LIMIT_SAMPLES) { |
85b5af06 UH |
165 | tmp_u64 = value; |
166 | limit_samples = *tmp_u64; | |
6239c175 | 167 | ret = SIGROK_OK; |
e15f48c2 BV |
168 | } else if (capability == HWCAP_PATTERN_MODE) { |
169 | stropt = value; | |
170 | if (!strcmp(stropt, "random")) { | |
171 | default_genmode = GENMODE_RANDOM; | |
172 | ret = SIGROK_OK; | |
173 | } else if (!strcmp(stropt, "incremental")) { | |
174 | default_genmode = GENMODE_INC; | |
175 | ret = SIGROK_OK; | |
176 | } else { | |
177 | ret = SIGROK_ERR; | |
178 | } | |
6239c175 UH |
179 | } else { |
180 | ret = SIGROK_ERR; | |
181 | } | |
182 | ||
183 | return ret; | |
184 | } | |
185 | ||
5096c6a6 | 186 | static void samples_generator(uint8_t *buf, uint64_t size, void *data) |
85b5af06 UH |
187 | { |
188 | struct databag *mydata = data; | |
189 | uint64_t i; | |
85b5af06 | 190 | |
5096c6a6 | 191 | memset(buf, 0, size); |
85b5af06 UH |
192 | |
193 | switch (mydata->sample_generator) { | |
194 | case GENMODE_RANDOM: /* Random */ | |
5096c6a6 UH |
195 | for (i = 0; i < size; i++) |
196 | *(buf + i) = (uint8_t)(rand() & 0xff); | |
85b5af06 UH |
197 | break; |
198 | case GENMODE_INC: /* Simple increment */ | |
5096c6a6 | 199 | for (i = 0; i < size; i++) |
85b5af06 UH |
200 | *(buf + i) = i; |
201 | break; | |
202 | } | |
203 | } | |
204 | ||
205 | /* Thread function */ | |
206 | static void thread_func(void *data) | |
207 | { | |
208 | struct databag *mydata = data; | |
209 | uint8_t buf[BUFSIZE]; | |
210 | uint64_t nb_to_send = 0; | |
211 | ||
212 | while (thread_running) { | |
ba3d481b OF |
213 | if (limit_samples) |
214 | nb_to_send = limit_samples - mydata->samples_counter; | |
215 | else | |
216 | nb_to_send = BUFSIZE; // CONTINUOUS MODE | |
85b5af06 UH |
217 | |
218 | if (nb_to_send == 0) { | |
219 | close(mydata->pipe_fds[1]); | |
220 | thread_running = 0; | |
221 | hw_stop_acquisition(mydata->device_index, | |
222 | mydata->session_device_id); | |
223 | } else if (nb_to_send > BUFSIZE) { | |
224 | nb_to_send = BUFSIZE; | |
225 | } | |
226 | ||
227 | samples_generator(buf, nb_to_send, data); | |
228 | mydata->samples_counter += nb_to_send; | |
229 | ||
230 | write(mydata->pipe_fds[1], &buf, nb_to_send); | |
231 | g_usleep(mydata->loop_sleep); | |
232 | } | |
233 | } | |
234 | ||
235 | /* Callback handling data */ | |
236 | static int receive_data(int fd, int revents, void *user_data) | |
237 | { | |
238 | struct datafeed_packet packet; | |
239 | /* uint16_t samples[1000]; */ | |
240 | char c[BUFSIZE]; | |
241 | uint64_t z; | |
242 | ||
243 | /* Avoid compiler warnings. */ | |
244 | revents = revents; | |
245 | ||
246 | z = read(fd, &c, BUFSIZE); | |
247 | if (z > 0) { | |
4c046c6b | 248 | packet.type = DF_LOGIC; |
85b5af06 | 249 | packet.length = z; |
4c046c6b | 250 | packet.unitsize = 1; |
85b5af06 UH |
251 | packet.payload = c; |
252 | session_bus(user_data, &packet); | |
253 | } | |
254 | return TRUE; | |
255 | } | |
256 | ||
6239c175 UH |
257 | static int hw_start_acquisition(int device_index, gpointer session_device_id) |
258 | { | |
259 | struct datafeed_packet *packet; | |
260 | struct datafeed_header *header; | |
85b5af06 | 261 | struct databag *mydata; |
6239c175 | 262 | |
85b5af06 | 263 | mydata = malloc(sizeof(struct databag)); |
5096c6a6 UH |
264 | if (!mydata) |
265 | return SIGROK_ERR_MALLOC; | |
85b5af06 | 266 | |
e15f48c2 | 267 | mydata->sample_generator = default_genmode; |
85b5af06 UH |
268 | mydata->session_device_id = session_device_id; |
269 | mydata->device_index = device_index; | |
270 | mydata->samples_counter = 0; | |
271 | mydata->loop_sleep = 100000; | |
272 | ||
e15f48c2 BV |
273 | if (pipe(mydata->pipe_fds)) |
274 | return SIGROK_ERR; | |
85b5af06 | 275 | |
85b5af06 UH |
276 | source_add(mydata->pipe_fds[0], G_IO_IN | G_IO_ERR, 40, receive_data, |
277 | session_device_id); | |
278 | ||
279 | /* Run the demo thread. */ | |
280 | g_thread_init(NULL); | |
281 | thread_running = 1; | |
282 | my_thread = | |
283 | g_thread_create((GThreadFunc)thread_func, mydata, TRUE, NULL); | |
e15f48c2 BV |
284 | if (!my_thread) |
285 | return SIGROK_ERR; | |
6239c175 UH |
286 | |
287 | packet = malloc(sizeof(struct datafeed_packet)); | |
288 | header = malloc(sizeof(struct datafeed_header)); | |
e15f48c2 | 289 | if (!packet || !header) |
6239c175 UH |
290 | return SIGROK_ERR_MALLOC; |
291 | ||
6239c175 UH |
292 | packet->type = DF_HEADER; |
293 | packet->length = sizeof(struct datafeed_header); | |
294 | packet->payload = (unsigned char *)header; | |
295 | header->feed_version = 1; | |
296 | gettimeofday(&header->starttime, NULL); | |
297 | header->samplerate = cur_samplerate; | |
298 | header->protocol_id = PROTO_RAW; | |
c2616fb9 DR |
299 | header->num_logic_probes = NUM_PROBES; |
300 | header->num_analog_probes = 0; | |
6239c175 UH |
301 | session_bus(session_device_id, packet); |
302 | free(header); | |
303 | free(packet); | |
304 | ||
305 | return SIGROK_OK; | |
306 | } | |
307 | ||
6239c175 UH |
308 | static void hw_stop_acquisition(int device_index, gpointer session_device_id) |
309 | { | |
310 | struct datafeed_packet packet; | |
311 | ||
17e1afcb | 312 | /* Avoid compiler warnings. */ |
6239c175 UH |
313 | device_index = device_index; |
314 | ||
85b5af06 | 315 | /* Send last packet. */ |
6239c175 UH |
316 | packet.type = DF_END; |
317 | session_bus(session_device_id, &packet); | |
e15f48c2 | 318 | |
6239c175 UH |
319 | } |
320 | ||
321 | struct device_plugin demo_plugin_info = { | |
322 | "demo", | |
323 | 1, | |
324 | hw_init, | |
325 | hw_cleanup, | |
326 | hw_opendev, | |
327 | hw_closedev, | |
328 | hw_get_device_info, | |
329 | hw_get_status, | |
330 | hw_get_capabilities, | |
331 | hw_set_configuration, | |
332 | hw_start_acquisition, | |
333 | hw_stop_acquisition, | |
334 | }; |