]> sigrok.org Git - libsigrok.git/blame_incremental - hardware/demo/demo.c
disable MSO-19 specific stuff for 0.1 release
[libsigrok.git] / hardware / demo / demo.c
... / ...
CommitLineData
1/*
2 * This file is part of the sigrok project.
3 *
4 * Copyright (C) 2010 Uwe Hermann <uwe@hermann-uwe.de>
5 * Copyright (C) 2011 Olivier Fauchon <olivier@aixmarseille.com>
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>
23#include <unistd.h>
24#include <string.h>
25#include <sigrok.h>
26#ifdef _WIN32
27#include <io.h>
28#include <fcntl.h>
29#define pipe(fds) _pipe(fds, 4096, _O_BINARY)
30#endif
31#include "config.h"
32
33#define NUM_PROBES 8
34#define DEMONAME "Demo device"
35/* size of chunks to send through the session bus */
36#define BUFSIZE 4096
37
38enum {
39 GENMODE_DEFAULT,
40 GENMODE_RANDOM,
41 GENMODE_INC,
42};
43
44GIOChannel *channels[2];
45
46struct databag {
47 int pipe_fds[2];
48 uint8_t sample_generator;
49 uint8_t thread_running;
50 uint64_t samples_counter;
51 int device_index;
52 int loop_sleep;
53 gpointer session_device_id;
54 GTimer *timer;
55};
56
57static int capabilities[] = {
58 HWCAP_LOGIC_ANALYZER,
59 HWCAP_PATTERN_MODE,
60 HWCAP_LIMIT_SAMPLES,
61 HWCAP_LIMIT_MSEC,
62 HWCAP_CONTINUOUS
63};
64
65static const char *patternmodes[] = {
66 "random",
67 "incremental",
68 NULL,
69};
70
71static uint8_t genmode_default[] = {
72 0x4c, 0x92, 0x92, 0x92, 0x64, 0x00, 0x00, 0x00,
73 0x82, 0xfe, 0xfe, 0x82, 0x00, 0x00, 0x00, 0x00,
74 0x7c, 0x82, 0x82, 0x92, 0x74, 0x00, 0x00, 0x00,
75 0xfe, 0x12, 0x12, 0x32, 0xcc, 0x00, 0x00, 0x00,
76 0x7c, 0x82, 0x82, 0x82, 0x7c, 0x00, 0x00, 0x00,
77 0xfe, 0x10, 0x28, 0x44, 0x82, 0x00, 0x00, 0x00,
78 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
79 0xbe, 0xbe, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
80};
81
82/* List of struct sigrok_device_instance, maintained by opendev()/closedev(). */
83static GSList *device_instances = NULL;
84static uint64_t cur_samplerate = 0;
85static uint64_t limit_samples = 0;
86static uint64_t limit_msec = 0;
87static int default_genmode = GENMODE_DEFAULT;
88static GThread *my_thread;
89static int thread_running;
90
91static void hw_stop_acquisition(int device_index, gpointer session_device_id);
92
93static int hw_init(char *deviceinfo)
94{
95 struct sigrok_device_instance *sdi;
96
97 /* Avoid compiler warnings. */
98 deviceinfo = deviceinfo;
99
100 sdi = sigrok_device_instance_new(0, ST_ACTIVE, DEMONAME, NULL, NULL);
101 if (!sdi)
102 return 0;
103
104 device_instances = g_slist_append(device_instances, sdi);
105
106 return 1;
107}
108
109static int hw_opendev(int device_index)
110{
111 /* Avoid compiler warnings. */
112 device_index = device_index;
113
114 /* Nothing needed so far. */
115 return SIGROK_OK;
116}
117
118static void hw_closedev(int device_index)
119{
120 /* Avoid compiler warnings. */
121 device_index = device_index;
122
123 /* Nothing needed so far. */
124}
125
126static void hw_cleanup(void)
127{
128 /* Nothing needed so far. */
129}
130
131static void *hw_get_device_info(int device_index, int device_info_id)
132{
133 struct sigrok_device_instance *sdi;
134 void *info = NULL;
135
136 if (!(sdi = get_sigrok_device_instance(device_instances, device_index)))
137 return NULL;
138
139 switch (device_info_id) {
140 case DI_INSTANCE:
141 info = sdi;
142 break;
143 case DI_NUM_PROBES:
144 info = GINT_TO_POINTER(NUM_PROBES);
145 break;
146 case DI_CUR_SAMPLERATE:
147 info = &cur_samplerate;
148 break;
149 case DI_PATTERNMODES:
150 info = &patternmodes;
151 break;
152 }
153
154 return info;
155}
156
157static int hw_get_status(int device_index)
158{
159 /* Avoid compiler warnings. */
160 device_index = device_index;
161
162 return ST_ACTIVE;
163}
164
165static int *hw_get_capabilities(void)
166{
167 return capabilities;
168}
169
170static int hw_set_configuration(int device_index, int capability, void *value)
171{
172 int ret;
173 uint64_t *tmp_u64;
174 char *stropt;
175
176 /* Avoid compiler warnings. */
177 device_index = device_index;
178
179 if (capability == HWCAP_PROBECONFIG) {
180 /* Nothing to do. */
181 ret = SIGROK_OK;
182 } else if (capability == HWCAP_LIMIT_SAMPLES) {
183 tmp_u64 = value;
184 limit_samples = *tmp_u64;
185 ret = SIGROK_OK;
186 } else if (capability == HWCAP_LIMIT_MSEC) {
187 tmp_u64 = value;
188 limit_msec = *tmp_u64;
189 ret = SIGROK_OK;
190 } else if (capability == HWCAP_PATTERN_MODE) {
191 stropt = value;
192 if (!strcmp(stropt, "random")) {
193 default_genmode = GENMODE_RANDOM;
194 ret = SIGROK_OK;
195 } else if (!strcmp(stropt, "incremental")) {
196 default_genmode = GENMODE_INC;
197 ret = SIGROK_OK;
198 } else {
199 ret = SIGROK_ERR;
200 }
201 } else {
202 ret = SIGROK_ERR;
203 }
204
205 return ret;
206}
207
208static void samples_generator(uint8_t *buf, uint64_t size, void *data)
209{
210 struct databag *mydata = data;
211 uint64_t p, i;
212
213 memset(buf, 0, size);
214
215 switch (mydata->sample_generator) {
216 case GENMODE_DEFAULT:
217 p = 0;
218 for (i = 0; i < size; i++) {
219 *(buf + i) = ~(genmode_default[p] >> 1);
220 if (++p == 64)
221 p = 0;
222 }
223 break;
224 case GENMODE_RANDOM: /* Random */
225 for (i = 0; i < size; i++)
226 *(buf + i) = (uint8_t)(rand() & 0xff);
227 break;
228 case GENMODE_INC: /* Simple increment */
229 for (i = 0; i < size; i++)
230 *(buf + i) = i;
231 break;
232 }
233}
234
235/* Thread function */
236static void thread_func(void *data)
237{
238 struct databag *mydata = data;
239 uint8_t buf[BUFSIZE];
240 uint64_t nb_to_send = 0;
241 int bytes_written;
242 unsigned int msec_elapsed;
243
244 while (thread_running) {
245 if (limit_samples)
246 nb_to_send = limit_samples - mydata->samples_counter;
247 else
248 nb_to_send = BUFSIZE; /* Continuous mode */
249
250 if (limit_msec) {
251 msec_elapsed = g_timer_elapsed(mydata->timer, NULL) * 1000;
252 if (msec_elapsed > limit_msec)
253 nb_to_send = 0;
254 }
255
256 if (nb_to_send == 0) {
257 close(mydata->pipe_fds[1]);
258 thread_running = 0;
259 hw_stop_acquisition(mydata->device_index,
260 mydata->session_device_id);
261 } else if (nb_to_send > BUFSIZE) {
262 nb_to_send = BUFSIZE;
263 }
264
265 samples_generator(buf, nb_to_send, data);
266 mydata->samples_counter += nb_to_send;
267
268 g_io_channel_write_chars(channels[1], (gchar *)&buf,
269 nb_to_send, (gsize *)&bytes_written, NULL);
270
271 g_usleep(mydata->loop_sleep);
272 }
273}
274
275/* Callback handling data */
276static int receive_data(int fd, int revents, void *user_data)
277{
278 struct datafeed_packet packet;
279 char c[BUFSIZE];
280 uint64_t z;
281
282 /* Avoid compiler warnings. */
283 fd = fd;
284 revents = revents;
285
286 g_io_channel_read_chars(channels[0], (gchar *)&c, BUFSIZE,
287 (gsize *)&z, NULL);
288
289 if (z > 0) {
290 packet.type = DF_LOGIC;
291 packet.length = z;
292 packet.unitsize = 1;
293 packet.payload = c;
294 session_bus(user_data, &packet);
295 }
296 return TRUE;
297}
298
299static int hw_start_acquisition(int device_index, gpointer session_device_id)
300{
301 struct datafeed_packet *packet;
302 struct datafeed_header *header;
303 struct databag *mydata;
304
305 mydata = malloc(sizeof(struct databag));
306 if (!mydata)
307 return SIGROK_ERR_MALLOC;
308
309 mydata->sample_generator = default_genmode;
310 mydata->session_device_id = session_device_id;
311 mydata->device_index = device_index;
312 mydata->samples_counter = 0;
313 mydata->loop_sleep = 100000;
314
315 if (pipe(mydata->pipe_fds))
316 return SIGROK_ERR;
317
318 channels[0] = g_io_channel_unix_new(mydata->pipe_fds[0]);
319 channels[1] = g_io_channel_unix_new(mydata->pipe_fds[1]);
320
321 /* Set channel encoding to binary (default is UTF-8). */
322 g_io_channel_set_encoding(channels[0], NULL, NULL);
323 g_io_channel_set_encoding(channels[1], NULL, NULL);
324
325 /* Make channels to unbuffered. */
326 g_io_channel_set_buffered(channels[0], FALSE);
327 g_io_channel_set_buffered(channels[1], FALSE);
328
329 source_add(mydata->pipe_fds[0], G_IO_IN | G_IO_ERR, 40, receive_data,
330 session_device_id);
331
332 /* Run the demo thread. */
333 g_thread_init(NULL);
334 if (limit_msec)
335 mydata->timer = g_timer_new();
336 thread_running = 1;
337 my_thread =
338 g_thread_create((GThreadFunc)thread_func, mydata, TRUE, NULL);
339 if (!my_thread)
340 return SIGROK_ERR;
341
342 packet = malloc(sizeof(struct datafeed_packet));
343 header = malloc(sizeof(struct datafeed_header));
344 if (!packet || !header)
345 return SIGROK_ERR_MALLOC;
346
347 packet->type = DF_HEADER;
348 packet->length = sizeof(struct datafeed_header);
349 packet->payload = (unsigned char *)header;
350 header->feed_version = 1;
351 gettimeofday(&header->starttime, NULL);
352 header->samplerate = cur_samplerate;
353 header->protocol_id = PROTO_RAW;
354 header->num_logic_probes = NUM_PROBES;
355 header->num_analog_probes = 0;
356 session_bus(session_device_id, packet);
357 free(header);
358 free(packet);
359
360 return SIGROK_OK;
361}
362
363static void hw_stop_acquisition(int device_index, gpointer session_device_id)
364{
365 struct datafeed_packet packet;
366
367 /* Avoid compiler warnings. */
368 device_index = device_index;
369
370 /* Send last packet. */
371 packet.type = DF_END;
372 session_bus(session_device_id, &packet);
373}
374
375struct device_plugin demo_plugin_info = {
376 "demo",
377 1,
378 hw_init,
379 hw_cleanup,
380 hw_opendev,
381 hw_closedev,
382 hw_get_device_info,
383 hw_get_status,
384 hw_get_capabilities,
385 hw_set_configuration,
386 hw_start_acquisition,
387 hw_stop_acquisition,
388};