]> sigrok.org Git - libsigrok.git/blame - hardware/demo/demo.c
cli: revamp option syntax
[libsigrok.git] / hardware / demo / demo.c
CommitLineData
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
8038e90a 22#define DEMO_ANALOG
764f887f 23
6239c175 24#include <stdlib.h>
85b5af06 25#include <unistd.h>
6239c175
UH
26#include <string.h>
27#include <sigrok.h>
8038e90a 28#include <math.h>
d35aaf02
UH
29#ifdef _WIN32
30#include <io.h>
31#include <fcntl.h>
32#define pipe(fds) _pipe(fds, 4096, _O_BINARY)
33#endif
6239c175
UH
34#include "config.h"
35
764f887f
DR
36#ifdef DEMO_ANALOG
37#define NUM_PROBES 9
38#else
e15f48c2 39#define NUM_PROBES 8
764f887f 40#endif
e15f48c2
BV
41#define DEMONAME "Demo device"
42/* size of chunks to send through the session bus */
764f887f
DR
43#ifdef DEMO_ANALOG
44#define BUFSIZE 32768
45#else
e15f48c2 46#define BUFSIZE 4096
764f887f 47#endif
85b5af06 48
e15f48c2 49enum {
917e0e71 50 GENMODE_DEFAULT,
e15f48c2
BV
51 GENMODE_RANDOM,
52 GENMODE_INC,
8038e90a 53 GENMODE_SINE,
e15f48c2 54};
85b5af06 55
d35aaf02
UH
56GIOChannel *channels[2];
57
e15f48c2
BV
58struct databag {
59 int pipe_fds[2];
60 uint8_t sample_generator;
61 uint8_t thread_running;
62 uint64_t samples_counter;
63 int device_index;
e15f48c2 64 gpointer session_device_id;
b9cc3629 65 GTimer *timer;
e15f48c2 66};
85b5af06 67
6239c175
UH
68static int capabilities[] = {
69 HWCAP_LOGIC_ANALYZER,
e15f48c2 70 HWCAP_PATTERN_MODE,
6239c175 71 HWCAP_LIMIT_SAMPLES,
b9cc3629 72 HWCAP_LIMIT_MSEC,
ba3d481b 73 HWCAP_CONTINUOUS
6239c175
UH
74};
75
02440dd8 76static const char *patternmodes[] = {
e15f48c2
BV
77 "random",
78 "incremental",
8038e90a 79 "sine",
02440dd8 80 NULL,
85b5af06
UH
81};
82
764f887f 83#ifndef DEMO_ANALOG
917e0e71
BV
84static uint8_t genmode_default[] = {
85 0x4c, 0x92, 0x92, 0x92, 0x64, 0x00, 0x00, 0x00,
86 0x82, 0xfe, 0xfe, 0x82, 0x00, 0x00, 0x00, 0x00,
87 0x7c, 0x82, 0x82, 0x92, 0x74, 0x00, 0x00, 0x00,
88 0xfe, 0x12, 0x12, 0x32, 0xcc, 0x00, 0x00, 0x00,
89 0x7c, 0x82, 0x82, 0x82, 0x7c, 0x00, 0x00, 0x00,
90 0xfe, 0x10, 0x28, 0x44, 0x82, 0x00, 0x00, 0x00,
91 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
92 0xbe, 0xbe, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
93};
764f887f 94#endif
917e0e71 95
85b5af06
UH
96/* List of struct sigrok_device_instance, maintained by opendev()/closedev(). */
97static GSList *device_instances = NULL;
1924f59f 98static uint64_t cur_samplerate = KHZ(200);
b9cc3629
BV
99static uint64_t limit_samples = 0;
100static uint64_t limit_msec = 0;
917e0e71 101static int default_genmode = GENMODE_DEFAULT;
b9cc3629
BV
102static GThread *my_thread;
103static int thread_running;
6239c175 104
6239c175
UH
105static void hw_stop_acquisition(int device_index, gpointer session_device_id);
106
6239c175
UH
107static int hw_init(char *deviceinfo)
108{
85b5af06
UH
109 struct sigrok_device_instance *sdi;
110
e15f48c2
BV
111 /* Avoid compiler warnings. */
112 deviceinfo = deviceinfo;
85b5af06 113
e15f48c2 114 sdi = sigrok_device_instance_new(0, ST_ACTIVE, DEMONAME, NULL, NULL);
85b5af06
UH
115 if (!sdi)
116 return 0;
e15f48c2 117
85b5af06
UH
118 device_instances = g_slist_append(device_instances, sdi);
119
120 return 1;
6239c175
UH
121}
122
123static int hw_opendev(int device_index)
124{
17e1afcb 125 /* Avoid compiler warnings. */
6239c175
UH
126 device_index = device_index;
127
128 /* Nothing needed so far. */
129 return SIGROK_OK;
130}
131
132static void hw_closedev(int device_index)
133{
17e1afcb 134 /* Avoid compiler warnings. */
6239c175
UH
135 device_index = device_index;
136
137 /* Nothing needed so far. */
138}
139
140static void hw_cleanup(void)
141{
142 /* Nothing needed so far. */
143}
144
145static void *hw_get_device_info(int device_index, int device_info_id)
146{
85b5af06
UH
147 struct sigrok_device_instance *sdi;
148 void *info = NULL;
149
150 if (!(sdi = get_sigrok_device_instance(device_instances, device_index)))
151 return NULL;
152
6239c175
UH
153 switch (device_info_id) {
154 case DI_INSTANCE:
85b5af06 155 info = sdi;
6239c175
UH
156 break;
157 case DI_NUM_PROBES:
158 info = GINT_TO_POINTER(NUM_PROBES);
159 break;
6239c175
UH
160 case DI_CUR_SAMPLERATE:
161 info = &cur_samplerate;
162 break;
e15f48c2
BV
163 case DI_PATTERNMODES:
164 info = &patternmodes;
165 break;
6239c175
UH
166 }
167
168 return info;
169}
170
171static int hw_get_status(int device_index)
172{
17e1afcb 173 /* Avoid compiler warnings. */
6239c175 174 device_index = device_index;
5096c6a6 175
e15f48c2 176 return ST_ACTIVE;
6239c175
UH
177}
178
179static int *hw_get_capabilities(void)
180{
181 return capabilities;
182}
183
184static int hw_set_configuration(int device_index, int capability, void *value)
185{
186 int ret;
85b5af06 187 uint64_t *tmp_u64;
e15f48c2 188 char *stropt;
6239c175 189
17e1afcb 190 /* Avoid compiler warnings. */
6239c175
UH
191 device_index = device_index;
192
e15f48c2 193 if (capability == HWCAP_PROBECONFIG) {
02440dd8 194 /* Nothing to do. */
85b5af06 195 ret = SIGROK_OK;
6239c175 196 } else if (capability == HWCAP_LIMIT_SAMPLES) {
85b5af06
UH
197 tmp_u64 = value;
198 limit_samples = *tmp_u64;
6239c175 199 ret = SIGROK_OK;
b9cc3629
BV
200 } else if (capability == HWCAP_LIMIT_MSEC) {
201 tmp_u64 = value;
202 limit_msec = *tmp_u64;
203 ret = SIGROK_OK;
e15f48c2
BV
204 } else if (capability == HWCAP_PATTERN_MODE) {
205 stropt = value;
206 if (!strcmp(stropt, "random")) {
207 default_genmode = GENMODE_RANDOM;
208 ret = SIGROK_OK;
209 } else if (!strcmp(stropt, "incremental")) {
210 default_genmode = GENMODE_INC;
211 ret = SIGROK_OK;
8038e90a
DR
212 } else if (!strcmp(stropt, "sine")) {
213 default_genmode = GENMODE_SINE;
214 ret = SIGROK_OK;
e15f48c2
BV
215 } else {
216 ret = SIGROK_ERR;
217 }
6239c175
UH
218 } else {
219 ret = SIGROK_ERR;
220 }
221
222 return ret;
223}
224
5096c6a6 225static void samples_generator(uint8_t *buf, uint64_t size, void *data)
85b5af06
UH
226{
227 struct databag *mydata = data;
917e0e71 228 uint64_t p, i;
764f887f
DR
229#ifdef DEMO_ANALOG
230 /*
231 * We will simulate a device with 8 logic probes and 1 analog probe.
232 * This fictional device sends the data packed: 8 bits for 8 logic
233 * probes and 16 bits for the analog probe, in this order.
234 * Total of 24 bits.
235 * I could just generate a properly formatted DF_ANALOG packet here,
236 * but I will leave the formatting to receive_data() to make its code
237 * more like a real hardware driver.
238 */
239 memset(buf, 0, size * 3);
240
241 switch (mydata->sample_generator) {
242 default:
243 case GENMODE_DEFAULT:
8038e90a
DR
244 case GENMODE_SINE:
245 for (i = 0; i < size * 3; i += 3) {
246 *(buf + i) = i / 3;
247 *(uint16_t *) (buf + i + 1) =
248 (uint16_t) (sin(i / 3) * 256 * 30);
249 }
250 break;
764f887f
DR
251 case GENMODE_RANDOM:
252 for (i = 0; i < size * 3; i += 3) {
253 *(buf + i) = (uint8_t)(rand() & 0xff);
254 *(uint16_t *) (buf + i + 1) = (uint16_t)(rand() & 0xffff);
255 }
256 break;
257 case GENMODE_INC:
258 for (i = 0; i < size * 3; i += 3) {
259 *(buf + i) = i / 3;
260 *(uint16_t *)(buf + i + 1) = i / 3 * 256 * 10;
261 }
262 break;
263 }
264#else
85b5af06 265
5096c6a6 266 memset(buf, 0, size);
85b5af06
UH
267
268 switch (mydata->sample_generator) {
917e0e71
BV
269 case GENMODE_DEFAULT:
270 p = 0;
271 for (i = 0; i < size; i++) {
272 *(buf + i) = ~(genmode_default[p] >> 1);
273 if (++p == 64)
274 p = 0;
275 }
276 break;
85b5af06 277 case GENMODE_RANDOM: /* Random */
5096c6a6
UH
278 for (i = 0; i < size; i++)
279 *(buf + i) = (uint8_t)(rand() & 0xff);
85b5af06
UH
280 break;
281 case GENMODE_INC: /* Simple increment */
5096c6a6 282 for (i = 0; i < size; i++)
85b5af06
UH
283 *(buf + i) = i;
284 break;
285 }
764f887f 286#endif
85b5af06
UH
287}
288
289/* Thread function */
290static void thread_func(void *data)
291{
292 struct databag *mydata = data;
293 uint8_t buf[BUFSIZE];
294 uint64_t nb_to_send = 0;
d35aaf02 295 int bytes_written;
1924f59f
HE
296
297 double time_cur, time_last, time_diff;
298
299 time_last = g_timer_elapsed(mydata->timer, NULL);
85b5af06
UH
300
301 while (thread_running) {
1924f59f
HE
302 /* Rate control */
303 time_cur = g_timer_elapsed(mydata->timer, NULL);
304
305 time_diff = time_cur - time_last;
306 time_last = time_cur;
307
308 nb_to_send = cur_samplerate * time_diff;
309
ba3d481b 310 if (limit_samples)
1924f59f
HE
311 nb_to_send = MIN(nb_to_send,
312 limit_samples - mydata->samples_counter);
313
314 /* Make sure we don't overflow. */
764f887f
DR
315#ifdef DEMO_ANALOG
316 nb_to_send = MIN(nb_to_send, BUFSIZE / 3);
317#else
1924f59f 318 nb_to_send = MIN(nb_to_send, BUFSIZE);
764f887f 319#endif
1924f59f
HE
320
321 if (nb_to_send) {
322 samples_generator(buf, nb_to_send, data);
323 mydata->samples_counter += nb_to_send;
764f887f
DR
324#ifdef DEMO_ANALOG
325 g_io_channel_write_chars(channels[1], (gchar *) &buf,
326 nb_to_send * 3, (gsize *) &bytes_written, NULL);
327#else
328 g_io_channel_write_chars(channels[1], (gchar *) &buf,
329 nb_to_send, (gsize *) &bytes_written, NULL);
330#endif
b9cc3629
BV
331 }
332
1924f59f
HE
333 /* Check if we're done. */
334 if ((limit_msec && time_cur * 1000 > limit_msec) ||
335 (limit_samples && mydata->samples_counter >= limit_samples))
336 {
85b5af06
UH
337 close(mydata->pipe_fds[1]);
338 thread_running = 0;
85b5af06
UH
339 }
340
1924f59f 341 g_usleep(10);
85b5af06
UH
342 }
343}
344
345/* Callback handling data */
346static int receive_data(int fd, int revents, void *user_data)
347{
26ce0bbf
UH
348 struct datafeed_packet packet;
349 char c[BUFSIZE];
350 uint64_t z;
764f887f
DR
351#ifdef DEMO_ANALOG
352 struct analog_sample *sample;
353 unsigned int i, x;
354 int sample_size = sizeof(struct analog_sample) +
355 (NUM_PROBES * sizeof(struct analog_probe));
356 char *buf;
357#endif
1924f59f 358
26ce0bbf
UH
359 /* Avoid compiler warnings. */
360 fd = fd;
361 revents = revents;
1924f59f
HE
362
363 do {
364 g_io_channel_read_chars(channels[0],
764f887f 365 (gchar *) &c, BUFSIZE, (gsize *) &z, NULL);
1924f59f
HE
366
367 if (z > 0) {
764f887f
DR
368#ifdef DEMO_ANALOG
369 packet.type = DF_ANALOG;
370
371 packet.length = (z / 3) * sample_size;
372 packet.unitsize = sample_size;
373
374 buf = malloc(sample_size * packet.length);
375 if (!buf)
376 return FALSE;
377
378 /* Craft our packet. */
379 for (i = 0; i < z / 3; i++) {
380 sample = (struct analog_sample *) (buf + (i * sample_size));
381 sample->num_probes = NUM_PROBES;
382
383 /* 8 Logic probes */
384 for (x = 0; x < NUM_PROBES - 1; x++) {
385 sample->probes[x].val =
386 (c[i * 3] >> x) & 1;
387 sample->probes[x].res = 1;
388 }
389
390 /* 1 Analog probe, 16 bit adc */
391 for (; x < NUM_PROBES; x++) {
392 sample->probes[x].val =
393 *(uint16_t *) (c + i * 3 + 1);
394 sample->probes[x].val &= ((1 << 16) - 1);
395 sample->probes[x].res = 16;
396 }
397
398 }
399
400 packet.payload = buf;
401 session_bus(user_data, &packet);
402 free(buf);
403#else
1924f59f
HE
404 packet.type = DF_LOGIC;
405 packet.length = z;
406 packet.unitsize = 1;
407 packet.payload = c;
408 session_bus(user_data, &packet);
764f887f 409#endif
1924f59f
HE
410 }
411 } while (z > 0);
85b5af06 412
1924f59f
HE
413 if (!thread_running && z <= 0)
414 {
415 /* Make sure we don't receive more packets */
416 g_io_channel_close(channels[0]);
d35aaf02 417
1924f59f
HE
418 /* Send last packet. */
419 packet.type = DF_END;
85b5af06 420 session_bus(user_data, &packet);
1924f59f
HE
421
422 return FALSE;
85b5af06 423 }
1924f59f 424
85b5af06
UH
425 return TRUE;
426}
427
6239c175
UH
428static int hw_start_acquisition(int device_index, gpointer session_device_id)
429{
430 struct datafeed_packet *packet;
431 struct datafeed_header *header;
85b5af06 432 struct databag *mydata;
6239c175 433
85b5af06 434 mydata = malloc(sizeof(struct databag));
5096c6a6
UH
435 if (!mydata)
436 return SIGROK_ERR_MALLOC;
85b5af06 437
e15f48c2 438 mydata->sample_generator = default_genmode;
85b5af06
UH
439 mydata->session_device_id = session_device_id;
440 mydata->device_index = device_index;
441 mydata->samples_counter = 0;
85b5af06 442
e15f48c2
BV
443 if (pipe(mydata->pipe_fds))
444 return SIGROK_ERR;
85b5af06 445
d35aaf02
UH
446 channels[0] = g_io_channel_unix_new(mydata->pipe_fds[0]);
447 channels[1] = g_io_channel_unix_new(mydata->pipe_fds[1]);
448
449 /* Set channel encoding to binary (default is UTF-8). */
450 g_io_channel_set_encoding(channels[0], NULL, NULL);
451 g_io_channel_set_encoding(channels[1], NULL, NULL);
452
453 /* Make channels to unbuffered. */
454 g_io_channel_set_buffered(channels[0], FALSE);
455 g_io_channel_set_buffered(channels[1], FALSE);
456
85b5af06
UH
457 source_add(mydata->pipe_fds[0], G_IO_IN | G_IO_ERR, 40, receive_data,
458 session_device_id);
459
460 /* Run the demo thread. */
461 g_thread_init(NULL);
1924f59f 462 mydata->timer = g_timer_new();
85b5af06
UH
463 thread_running = 1;
464 my_thread =
465 g_thread_create((GThreadFunc)thread_func, mydata, TRUE, NULL);
e15f48c2
BV
466 if (!my_thread)
467 return SIGROK_ERR;
6239c175
UH
468
469 packet = malloc(sizeof(struct datafeed_packet));
470 header = malloc(sizeof(struct datafeed_header));
e15f48c2 471 if (!packet || !header)
6239c175
UH
472 return SIGROK_ERR_MALLOC;
473
6239c175
UH
474 packet->type = DF_HEADER;
475 packet->length = sizeof(struct datafeed_header);
476 packet->payload = (unsigned char *)header;
764f887f
DR
477#ifdef DEMO_ANALOG
478 packet->unitsize = sizeof(struct analog_sample) +
479 (NUM_PROBES * sizeof(struct analog_probe));
480#endif
6239c175
UH
481 header->feed_version = 1;
482 gettimeofday(&header->starttime, NULL);
483 header->samplerate = cur_samplerate;
484 header->protocol_id = PROTO_RAW;
c2616fb9
DR
485 header->num_logic_probes = NUM_PROBES;
486 header->num_analog_probes = 0;
6239c175
UH
487 session_bus(session_device_id, packet);
488 free(header);
489 free(packet);
490
491 return SIGROK_OK;
492}
493
6239c175
UH
494static void hw_stop_acquisition(int device_index, gpointer session_device_id)
495{
17e1afcb 496 /* Avoid compiler warnings. */
6239c175 497 device_index = device_index;
1924f59f 498 session_device_id = session_device_id;
6239c175 499
1924f59f
HE
500 /* Stop generate thread. */
501 thread_running = 0;
6239c175
UH
502}
503
504struct device_plugin demo_plugin_info = {
505 "demo",
506 1,
507 hw_init,
508 hw_cleanup,
509 hw_opendev,
510 hw_closedev,
511 hw_get_device_info,
512 hw_get_status,
513 hw_get_capabilities,
514 hw_set_configuration,
515 hw_start_acquisition,
516 hw_stop_acquisition,
517};