]> sigrok.org Git - libsigrok.git/blob - src/hardware/demo/demo.c
Build: Include <config.h> first in all source files
[libsigrok.git] / src / hardware / demo / demo.c
1 /*
2  * This file is part of the libsigrok project.
3  *
4  * Copyright (C) 2010 Uwe Hermann <uwe@hermann-uwe.de>
5  * Copyright (C) 2011 Olivier Fauchon <olivier@aixmarseille.com>
6  * Copyright (C) 2012 Alexandru Gagniuc <mr.nuke.me@gmail.com>
7  * Copyright (C) 2015 Bartosz Golaszewski <bgolaszewski@baylibre.com>
8  *
9  * This program is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program; if not, write to the Free Software
21  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301 USA
22  */
23
24 #include <config.h>
25 #include <stdlib.h>
26 #include <string.h>
27 #include <math.h>
28 #include <libsigrok/libsigrok.h>
29 #include "libsigrok-internal.h"
30
31 #define LOG_PREFIX "demo"
32
33 #define DEFAULT_NUM_LOGIC_CHANNELS     8
34 #define DEFAULT_NUM_ANALOG_CHANNELS    4
35
36 /* The size in bytes of chunks to send through the session bus. */
37 #define LOGIC_BUFSIZE        4096
38 /* Size of the analog pattern space per channel. */
39 #define ANALOG_BUFSIZE       4096
40
41 #define DEFAULT_ANALOG_AMPLITUDE 25
42 #define ANALOG_SAMPLES_PER_PERIOD 20
43
44 /* Logic patterns we can generate. */
45 enum {
46         /**
47          * Spells "sigrok" across 8 channels using '0's (with '1's as
48          * "background") when displayed using the 'bits' output format.
49          * The pattern is repeated every 8 channels, shifted to the right
50          * in time by one bit.
51          */
52         PATTERN_SIGROK,
53
54         /** Pseudo-random values on all channels. */
55         PATTERN_RANDOM,
56
57         /**
58          * Incrementing number across 8 channels. The pattern is repeated
59          * every 8 channels, shifted to the right in time by one bit.
60          */
61         PATTERN_INC,
62
63         /** All channels have a low logic state. */
64         PATTERN_ALL_LOW,
65
66         /** All channels have a high logic state. */
67         PATTERN_ALL_HIGH,
68 };
69
70 /* Analog patterns we can generate. */
71 enum {
72         /**
73          * Square wave.
74          */
75         PATTERN_SQUARE,
76         PATTERN_SINE,
77         PATTERN_TRIANGLE,
78         PATTERN_SAWTOOTH,
79 };
80
81 static const char *logic_pattern_str[] = {
82         "sigrok",
83         "random",
84         "incremental",
85         "all-low",
86         "all-high",
87 };
88
89 static const char *analog_pattern_str[] = {
90         "square",
91         "sine",
92         "triangle",
93         "sawtooth",
94 };
95
96 struct analog_gen {
97         int pattern;
98         float amplitude;
99         float pattern_data[ANALOG_BUFSIZE];
100         unsigned int num_samples;
101         struct sr_datafeed_analog packet;
102         float avg_val; /* Average value */
103         unsigned num_avgs; /* Number of samples averaged */
104 };
105
106 /* Private, per-device-instance driver context. */
107 struct dev_context {
108         uint64_t cur_samplerate;
109         gboolean continuous;
110         uint64_t limit_samples;
111         uint64_t limit_msec;
112         uint64_t logic_counter;
113         uint64_t analog_counter;
114         int64_t starttime;
115         uint64_t step;
116         /* Logic */
117         int32_t num_logic_channels;
118         unsigned int logic_unitsize;
119         /* There is only ever one logic channel group, so its pattern goes here. */
120         uint8_t logic_pattern;
121         unsigned char logic_data[LOGIC_BUFSIZE];
122         /* Analog */
123         int32_t num_analog_channels;
124         GHashTable *ch_ag;
125         gboolean avg; /* True if averaging is enabled */
126         uint64_t avg_samples;
127 };
128
129 static const uint32_t drvopts[] = {
130         SR_CONF_DEMO_DEV,
131         SR_CONF_LOGIC_ANALYZER,
132         SR_CONF_OSCILLOSCOPE,
133 };
134
135 static const uint32_t scanopts[] = {
136         SR_CONF_NUM_LOGIC_CHANNELS,
137         SR_CONF_NUM_ANALOG_CHANNELS,
138 };
139
140 static const uint32_t devopts[] = {
141         SR_CONF_CONTINUOUS | SR_CONF_SET,
142         SR_CONF_LIMIT_SAMPLES | SR_CONF_GET | SR_CONF_SET,
143         SR_CONF_LIMIT_MSEC | SR_CONF_GET | SR_CONF_SET,
144         SR_CONF_SAMPLERATE | SR_CONF_GET | SR_CONF_SET | SR_CONF_LIST,
145         SR_CONF_AVERAGING | SR_CONF_GET | SR_CONF_SET,
146         SR_CONF_AVG_SAMPLES | SR_CONF_GET | SR_CONF_SET,
147 };
148
149 static const uint32_t devopts_cg_logic[] = {
150         SR_CONF_PATTERN_MODE | SR_CONF_GET | SR_CONF_SET | SR_CONF_LIST,
151 };
152
153 static const uint32_t devopts_cg_analog_group[] = {
154         SR_CONF_AMPLITUDE | SR_CONF_GET | SR_CONF_SET,
155 };
156
157 static const uint32_t devopts_cg_analog_channel[] = {
158         SR_CONF_PATTERN_MODE | SR_CONF_GET | SR_CONF_SET | SR_CONF_LIST,
159         SR_CONF_AMPLITUDE | SR_CONF_GET | SR_CONF_SET,
160 };
161
162 static const uint64_t samplerates[] = {
163         SR_HZ(1),
164         SR_GHZ(1),
165         SR_HZ(1),
166 };
167
168 static const uint8_t pattern_sigrok[] = {
169         0x4c, 0x92, 0x92, 0x92, 0x64, 0x00, 0x00, 0x00,
170         0x82, 0xfe, 0xfe, 0x82, 0x00, 0x00, 0x00, 0x00,
171         0x7c, 0x82, 0x82, 0x92, 0x74, 0x00, 0x00, 0x00,
172         0xfe, 0x12, 0x12, 0x32, 0xcc, 0x00, 0x00, 0x00,
173         0x7c, 0x82, 0x82, 0x82, 0x7c, 0x00, 0x00, 0x00,
174         0xfe, 0x10, 0x28, 0x44, 0x82, 0x00, 0x00, 0x00,
175         0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
176         0xbe, 0xbe, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
177 };
178
179 SR_PRIV struct sr_dev_driver demo_driver_info;
180
181 static int dev_acquisition_stop(struct sr_dev_inst *sdi, void *cb_data);
182
183 static int init(struct sr_dev_driver *di, struct sr_context *sr_ctx)
184 {
185         return std_init(sr_ctx, di, LOG_PREFIX);
186 }
187
188 static void generate_analog_pattern(struct analog_gen *ag, uint64_t sample_rate)
189 {
190         double t, frequency;
191         float value;
192         unsigned int num_samples, i;
193         int last_end;
194
195         sr_dbg("Generating %s pattern.", analog_pattern_str[ag->pattern]);
196
197         num_samples = ANALOG_BUFSIZE / sizeof(float);
198
199         switch (ag->pattern) {
200         case PATTERN_SQUARE:
201                 value = ag->amplitude;
202                 last_end = 0;
203                 for (i = 0; i < num_samples; i++) {
204                         if (i % 5 == 0)
205                                 value = -value;
206                         if (i % 10 == 0)
207                                 last_end = i;
208                         ag->pattern_data[i] = value;
209                 }
210                 ag->num_samples = last_end;
211                 break;
212         case PATTERN_SINE:
213                 frequency = (double) sample_rate / ANALOG_SAMPLES_PER_PERIOD;
214
215                 /* Make sure the number of samples we put out is an integer
216                  * multiple of our period size */
217                 /* FIXME we actually need only one period. A ringbuffer would be
218                  * useful here. */
219                 while (num_samples % ANALOG_SAMPLES_PER_PERIOD != 0)
220                         num_samples--;
221
222                 for (i = 0; i < num_samples; i++) {
223                         t = (double) i / (double) sample_rate;
224                         ag->pattern_data[i] = ag->amplitude *
225                                                 sin(2 * G_PI * frequency * t);
226                 }
227
228                 ag->num_samples = num_samples;
229                 break;
230         case PATTERN_TRIANGLE:
231                 frequency = (double) sample_rate / ANALOG_SAMPLES_PER_PERIOD;
232
233                 while (num_samples % ANALOG_SAMPLES_PER_PERIOD != 0)
234                         num_samples--;
235
236                 for (i = 0; i < num_samples; i++) {
237                         t = (double) i / (double) sample_rate;
238                         ag->pattern_data[i] = (2 * ag->amplitude / G_PI) *
239                                                 asin(sin(2 * G_PI * frequency * t));
240                 }
241
242                 ag->num_samples = num_samples;
243                 break;
244         case PATTERN_SAWTOOTH:
245                 frequency = (double) sample_rate / ANALOG_SAMPLES_PER_PERIOD;
246
247                 while (num_samples % ANALOG_SAMPLES_PER_PERIOD != 0)
248                         num_samples--;
249
250                 for (i = 0; i < num_samples; i++) {
251                         t = (double) i / (double) sample_rate;
252                         ag->pattern_data[i] = 2 * ag->amplitude *
253                                                 ((t * frequency) - floor(0.5f + t * frequency));
254                 }
255
256                 ag->num_samples = num_samples;
257                 break;
258         }
259 }
260
261 static GSList *scan(struct sr_dev_driver *di, GSList *options)
262 {
263         struct drv_context *drvc;
264         struct dev_context *devc;
265         struct sr_dev_inst *sdi;
266         struct sr_channel *ch;
267         struct sr_channel_group *cg, *acg;
268         struct sr_config *src;
269         struct analog_gen *ag;
270         GSList *devices, *l;
271         int num_logic_channels, num_analog_channels, pattern, i;
272         char channel_name[16];
273
274         drvc = di->context;
275
276         num_logic_channels = DEFAULT_NUM_LOGIC_CHANNELS;
277         num_analog_channels = DEFAULT_NUM_ANALOG_CHANNELS;
278         for (l = options; l; l = l->next) {
279                 src = l->data;
280                 switch (src->key) {
281                 case SR_CONF_NUM_LOGIC_CHANNELS:
282                         num_logic_channels = g_variant_get_int32(src->data);
283                         break;
284                 case SR_CONF_NUM_ANALOG_CHANNELS:
285                         num_analog_channels = g_variant_get_int32(src->data);
286                         break;
287                 }
288         }
289
290         devices = NULL;
291
292         sdi = g_malloc0(sizeof(struct sr_dev_inst));
293         sdi->status = SR_ST_ACTIVE;
294         sdi->model = g_strdup("Demo device");
295         sdi->driver = di;
296
297         devc = g_malloc(sizeof(struct dev_context));
298         devc->cur_samplerate = SR_KHZ(200);
299         devc->limit_samples = 0;
300         devc->limit_msec = 0;
301         devc->step = 0;
302         devc->continuous = FALSE;
303         devc->num_logic_channels = num_logic_channels;
304         devc->logic_unitsize = (devc->num_logic_channels + 7) / 8;
305         devc->logic_pattern = PATTERN_SIGROK;
306         devc->num_analog_channels = num_analog_channels;
307         devc->avg = FALSE;
308         devc->avg_samples = 0;
309
310         /* Logic channels, all in one channel group. */
311         cg = g_malloc0(sizeof(struct sr_channel_group));
312         cg->name = g_strdup("Logic");
313         for (i = 0; i < num_logic_channels; i++) {
314                 sprintf(channel_name, "D%d", i);
315                 ch = sr_channel_new(sdi, i, SR_CHANNEL_LOGIC, TRUE, channel_name);
316                 cg->channels = g_slist_append(cg->channels, ch);
317         }
318         sdi->channel_groups = g_slist_append(NULL, cg);
319
320         /* Analog channels, channel groups and pattern generators. */
321         pattern = 0;
322         /* An "Analog" channel group with all analog channels in it. */
323         acg = g_malloc0(sizeof(struct sr_channel_group));
324         acg->name = g_strdup("Analog");
325         sdi->channel_groups = g_slist_append(sdi->channel_groups, acg);
326
327         devc->ch_ag = g_hash_table_new(g_direct_hash, g_direct_equal);
328         for (i = 0; i < num_analog_channels; i++) {
329                 snprintf(channel_name, 16, "A%d", i);
330                 ch = sr_channel_new(sdi, i + num_logic_channels, SR_CHANNEL_ANALOG,
331                                 TRUE, channel_name);
332                 acg->channels = g_slist_append(acg->channels, ch);
333
334                 /* Every analog channel gets its own channel group as well. */
335                 cg = g_malloc0(sizeof(struct sr_channel_group));
336                 cg->name = g_strdup(channel_name);
337                 cg->channels = g_slist_append(NULL, ch);
338                 sdi->channel_groups = g_slist_append(sdi->channel_groups, cg);
339
340                 /* Every channel gets a generator struct. */
341                 ag = g_malloc(sizeof(struct analog_gen));
342                 ag->amplitude = DEFAULT_ANALOG_AMPLITUDE;
343                 ag->packet.channels = cg->channels;
344                 ag->packet.mq = 0;
345                 ag->packet.mqflags = 0;
346                 ag->packet.unit = SR_UNIT_VOLT;
347                 ag->packet.data = ag->pattern_data;
348                 ag->pattern = pattern;
349                 ag->avg_val = 0.0f;
350                 ag->num_avgs = 0;
351                 g_hash_table_insert(devc->ch_ag, ch, ag);
352
353                 if (++pattern == ARRAY_SIZE(analog_pattern_str))
354                         pattern = 0;
355         }
356
357         sdi->priv = devc;
358         devices = g_slist_append(devices, sdi);
359         drvc->instances = g_slist_append(drvc->instances, sdi);
360
361         return devices;
362 }
363
364 static GSList *dev_list(const struct sr_dev_driver *di)
365 {
366         return ((struct drv_context *)(di->context))->instances;
367 }
368
369 static int dev_open(struct sr_dev_inst *sdi)
370 {
371         sdi->status = SR_ST_ACTIVE;
372
373         return SR_OK;
374 }
375
376 static int dev_close(struct sr_dev_inst *sdi)
377 {
378         sdi->status = SR_ST_INACTIVE;
379
380         return SR_OK;
381 }
382
383 static void clear_helper(void *priv)
384 {
385         struct dev_context *devc;
386         GHashTableIter iter;
387         void *value;
388
389         devc = priv;
390
391         /* Analog generators. */
392         g_hash_table_iter_init(&iter, devc->ch_ag);
393         while (g_hash_table_iter_next(&iter, NULL, &value))
394                 g_free(value);
395         g_hash_table_unref(devc->ch_ag);
396         g_free(devc);
397 }
398
399 static int cleanup(const struct sr_dev_driver *di)
400 {
401         return std_dev_clear(di, clear_helper);
402 }
403
404 static int config_get(uint32_t key, GVariant **data, const struct sr_dev_inst *sdi,
405                 const struct sr_channel_group *cg)
406 {
407         struct dev_context *devc;
408         struct sr_channel *ch;
409         struct analog_gen *ag;
410         int pattern;
411
412         if (!sdi)
413                 return SR_ERR_ARG;
414
415         devc = sdi->priv;
416         switch (key) {
417         case SR_CONF_SAMPLERATE:
418                 *data = g_variant_new_uint64(devc->cur_samplerate);
419                 break;
420         case SR_CONF_LIMIT_SAMPLES:
421                 *data = g_variant_new_uint64(devc->limit_samples);
422                 break;
423         case SR_CONF_LIMIT_MSEC:
424                 *data = g_variant_new_uint64(devc->limit_msec);
425                 break;
426         case SR_CONF_AVERAGING:
427                 *data = g_variant_new_boolean(devc->avg);
428                 break;
429         case SR_CONF_AVG_SAMPLES:
430                 *data = g_variant_new_uint64(devc->avg_samples);
431                 break;
432         case SR_CONF_PATTERN_MODE:
433                 if (!cg)
434                         return SR_ERR_CHANNEL_GROUP;
435                 /* Any channel in the group will do. */
436                 ch = cg->channels->data;
437                 if (ch->type == SR_CHANNEL_LOGIC) {
438                         pattern = devc->logic_pattern;
439                         *data = g_variant_new_string(logic_pattern_str[pattern]);
440                 } else if (ch->type == SR_CHANNEL_ANALOG) {
441                         ag = g_hash_table_lookup(devc->ch_ag, ch);
442                         pattern = ag->pattern;
443                         *data = g_variant_new_string(analog_pattern_str[pattern]);
444                 } else
445                         return SR_ERR_BUG;
446                 break;
447         case SR_CONF_AMPLITUDE:
448                 if (!cg)
449                         return SR_ERR_CHANNEL_GROUP;
450                 /* Any channel in the group will do. */
451                 ch = cg->channels->data;
452                 if (ch->type != SR_CHANNEL_ANALOG)
453                         return SR_ERR_ARG;
454                 ag = g_hash_table_lookup(devc->ch_ag, ch);
455                 *data = g_variant_new_double(ag->amplitude);
456                 break;
457         default:
458                 return SR_ERR_NA;
459         }
460
461         return SR_OK;
462 }
463
464 static int config_set(uint32_t key, GVariant *data, const struct sr_dev_inst *sdi,
465                 const struct sr_channel_group *cg)
466 {
467         struct dev_context *devc;
468         struct analog_gen *ag;
469         struct sr_channel *ch;
470         GSList *l;
471         int logic_pattern, analog_pattern, ret;
472         unsigned int i;
473         const char *stropt;
474
475         devc = sdi->priv;
476
477         if (sdi->status != SR_ST_ACTIVE)
478                 return SR_ERR_DEV_CLOSED;
479
480         ret = SR_OK;
481         switch (key) {
482         case SR_CONF_SAMPLERATE:
483                 devc->cur_samplerate = g_variant_get_uint64(data);
484                 break;
485         case SR_CONF_LIMIT_SAMPLES:
486                 devc->limit_msec = 0;
487                 devc->limit_samples = g_variant_get_uint64(data);
488                 break;
489         case SR_CONF_LIMIT_MSEC:
490                 devc->limit_msec = g_variant_get_uint64(data);
491                 devc->limit_samples = 0;
492                 break;
493         case SR_CONF_AVERAGING:
494                 devc->avg = g_variant_get_boolean(data);
495                 sr_dbg("%s averaging", devc->avg ? "Enabling" : "Disabling");
496                 break;
497         case SR_CONF_AVG_SAMPLES:
498                 devc->avg_samples = g_variant_get_uint64(data);
499                 sr_dbg("Setting averaging rate to %" PRIu64, devc->avg_samples);
500                 break;
501         case SR_CONF_PATTERN_MODE:
502                 if (!cg)
503                         return SR_ERR_CHANNEL_GROUP;
504                 stropt = g_variant_get_string(data, NULL);
505                 logic_pattern = analog_pattern = -1;
506                 for (i = 0; i < ARRAY_SIZE(logic_pattern_str); i++) {
507                         if (!strcmp(stropt, logic_pattern_str[i])) {
508                                 logic_pattern = i;
509                                 break;
510                         }
511                 }
512                 for (i = 0; i < ARRAY_SIZE(analog_pattern_str); i++) {
513                         if (!strcmp(stropt, analog_pattern_str[i])) {
514                                 analog_pattern = i;
515                                 break;
516                         }
517                 }
518                 if (logic_pattern == -1 && analog_pattern == -1)
519                         return SR_ERR_ARG;
520                 for (l = cg->channels; l; l = l->next) {
521                         ch = l->data;
522                         if (ch->type == SR_CHANNEL_LOGIC) {
523                                 if (logic_pattern == -1)
524                                         return SR_ERR_ARG;
525                                 sr_dbg("Setting logic pattern to %s",
526                                                 logic_pattern_str[logic_pattern]);
527                                 devc->logic_pattern = logic_pattern;
528                                 /* Might as well do this now, these are static. */
529                                 if (logic_pattern == PATTERN_ALL_LOW)
530                                         memset(devc->logic_data, 0x00, LOGIC_BUFSIZE);
531                                 else if (logic_pattern == PATTERN_ALL_HIGH)
532                                         memset(devc->logic_data, 0xff, LOGIC_BUFSIZE);
533                         } else if (ch->type == SR_CHANNEL_ANALOG) {
534                                 if (analog_pattern == -1)
535                                         return SR_ERR_ARG;
536                                 sr_dbg("Setting analog pattern for channel %s to %s",
537                                                 ch->name, analog_pattern_str[analog_pattern]);
538                                 ag = g_hash_table_lookup(devc->ch_ag, ch);
539                                 ag->pattern = analog_pattern;
540                         } else
541                                 return SR_ERR_BUG;
542                 }
543                 break;
544         case SR_CONF_AMPLITUDE:
545                 if (!cg)
546                         return SR_ERR_CHANNEL_GROUP;
547                 for (l = cg->channels; l; l = l->next) {
548                         ch = l->data;
549                         if (ch->type != SR_CHANNEL_ANALOG)
550                                 return SR_ERR_ARG;
551                         ag = g_hash_table_lookup(devc->ch_ag, ch);
552                         ag->amplitude = g_variant_get_double(data);
553                 }
554                 break;
555         default:
556                 ret = SR_ERR_NA;
557         }
558
559         return ret;
560 }
561
562 static int config_list(uint32_t key, GVariant **data, const struct sr_dev_inst *sdi,
563                 const struct sr_channel_group *cg)
564 {
565         struct sr_channel *ch;
566         GVariant *gvar;
567         GVariantBuilder gvb;
568
569         if (key == SR_CONF_SCAN_OPTIONS) {
570                 *data = g_variant_new_fixed_array(G_VARIANT_TYPE_UINT32,
571                                 scanopts, ARRAY_SIZE(scanopts), sizeof(uint32_t));
572                 return SR_OK;
573         }
574
575         if (key == SR_CONF_DEVICE_OPTIONS && !sdi) {
576                 *data = g_variant_new_fixed_array(G_VARIANT_TYPE_UINT32,
577                                 drvopts, ARRAY_SIZE(drvopts), sizeof(uint32_t));
578                 return SR_OK;
579         }
580
581         if (!sdi)
582                 return SR_ERR_ARG;
583
584         if (!cg) {
585                 switch (key) {
586                 case SR_CONF_DEVICE_OPTIONS:
587                         *data = g_variant_new_fixed_array(G_VARIANT_TYPE_UINT32,
588                                         devopts, ARRAY_SIZE(devopts), sizeof(uint32_t));
589                         break;
590                 case SR_CONF_SAMPLERATE:
591                         g_variant_builder_init(&gvb, G_VARIANT_TYPE("a{sv}"));
592                         gvar = g_variant_new_fixed_array(G_VARIANT_TYPE("t"), samplerates,
593                                         ARRAY_SIZE(samplerates), sizeof(uint64_t));
594                         g_variant_builder_add(&gvb, "{sv}", "samplerate-steps", gvar);
595                         *data = g_variant_builder_end(&gvb);
596                         break;
597                 default:
598                         return SR_ERR_NA;
599                 }
600         } else {
601                 ch = cg->channels->data;
602                 switch (key) {
603                 case SR_CONF_DEVICE_OPTIONS:
604                         if (ch->type == SR_CHANNEL_LOGIC)
605                                 *data = g_variant_new_fixed_array(G_VARIANT_TYPE_UINT32,
606                                                 devopts_cg_logic, ARRAY_SIZE(devopts_cg_logic),
607                                                 sizeof(uint32_t));
608                         else if (ch->type == SR_CHANNEL_ANALOG) {
609                                 if (strcmp(cg->name, "Analog") == 0)
610                                         *data = g_variant_new_fixed_array(G_VARIANT_TYPE_UINT32,
611                                                         devopts_cg_analog_group, ARRAY_SIZE(devopts_cg_analog_group),
612                                                         sizeof(uint32_t));
613                                 else
614                                         *data = g_variant_new_fixed_array(G_VARIANT_TYPE_UINT32,
615                                                         devopts_cg_analog_channel, ARRAY_SIZE(devopts_cg_analog_channel),
616                                                         sizeof(uint32_t));
617                         }
618                         else
619                                 return SR_ERR_BUG;
620                         break;
621                 case SR_CONF_PATTERN_MODE:
622                         /* The analog group (with all 4 channels) shall not have a pattern property. */
623                         if (strcmp(cg->name, "Analog") == 0)
624                                 return SR_ERR_NA;
625
626                         if (ch->type == SR_CHANNEL_LOGIC)
627                                 *data = g_variant_new_strv(logic_pattern_str,
628                                                 ARRAY_SIZE(logic_pattern_str));
629                         else if (ch->type == SR_CHANNEL_ANALOG)
630                                 *data = g_variant_new_strv(analog_pattern_str,
631                                                 ARRAY_SIZE(analog_pattern_str));
632                         else
633                                 return SR_ERR_BUG;
634                         break;
635                 default:
636                         return SR_ERR_NA;
637                 }
638         }
639
640         return SR_OK;
641 }
642
643 static void logic_generator(struct sr_dev_inst *sdi, uint64_t size)
644 {
645         struct dev_context *devc;
646         uint64_t i, j;
647         uint8_t pat;
648
649         devc = sdi->priv;
650
651         switch (devc->logic_pattern) {
652         case PATTERN_SIGROK:
653                 memset(devc->logic_data, 0x00, size);
654                 for (i = 0; i < size; i += devc->logic_unitsize) {
655                         for (j = 0; j < devc->logic_unitsize; j++) {
656                                 pat = pattern_sigrok[(devc->step + j) % sizeof(pattern_sigrok)] >> 1;
657                                 devc->logic_data[i + j] = ~pat;
658                         }
659                         devc->step++;
660                 }
661                 break;
662         case PATTERN_RANDOM:
663                 for (i = 0; i < size; i++)
664                         devc->logic_data[i] = (uint8_t)(rand() & 0xff);
665                 break;
666         case PATTERN_INC:
667                 for (i = 0; i < size; i++) {
668                         for (j = 0; j < devc->logic_unitsize; j++) {
669                                 devc->logic_data[i + j] = devc->step;
670                         }
671                         devc->step++;
672                 }
673                 break;
674         case PATTERN_ALL_LOW:
675         case PATTERN_ALL_HIGH:
676                 /* These were set when the pattern mode was selected. */
677                 break;
678         default:
679                 sr_err("Unknown pattern: %d.", devc->logic_pattern);
680                 break;
681         }
682 }
683
684 static void send_analog_packet(struct analog_gen *ag,
685                                struct sr_dev_inst *sdi,
686                                uint64_t *analog_sent,
687                                uint64_t analog_todo)
688 {
689         struct sr_datafeed_packet packet;
690         struct dev_context *devc;
691         uint64_t sending_now, to_avg;
692         int ag_pattern_pos;
693         unsigned int i;
694
695         devc = sdi->priv;
696         packet.type = SR_DF_ANALOG;
697         packet.payload = &ag->packet;
698
699         if (!devc->avg) {
700                 ag_pattern_pos = devc->analog_counter % ag->num_samples;
701                 sending_now = MIN(analog_todo, ag->num_samples-ag_pattern_pos);
702                 ag->packet.data = ag->pattern_data + ag_pattern_pos;
703                 ag->packet.num_samples = sending_now;
704                 sr_session_send(sdi, &packet);
705
706                 /* Whichever channel group gets there first. */
707                 *analog_sent = MAX(*analog_sent, sending_now);
708         } else {
709                 ag_pattern_pos = devc->analog_counter % ag->num_samples;
710                 to_avg = MIN(analog_todo, ag->num_samples-ag_pattern_pos);
711
712                 for (i = 0; i < to_avg; i++) {
713                         ag->avg_val = (ag->avg_val +
714                                         *(ag->pattern_data +
715                                           ag_pattern_pos + i)) / 2;
716                         ag->num_avgs++;
717                         /* Time to send averaged data? */
718                         if (devc->avg_samples > 0 &&
719                             ag->num_avgs >= devc->avg_samples)
720                                 goto do_send;
721                 }
722
723                 if (devc->avg_samples == 0) {
724                         /* We're averaging all the samples, so wait with
725                          * sending until the very end.
726                          */
727                         *analog_sent = ag->num_avgs;
728                         return;
729                 }
730
731 do_send:
732                 ag->packet.data = &ag->avg_val;
733                 ag->packet.num_samples = 1;
734
735                 sr_session_send(sdi, &packet);
736                 *analog_sent = ag->num_avgs;
737
738                 ag->num_avgs = 0;
739                 ag->avg_val = 0.0f;
740         }
741 }
742
743 /* Callback handling data */
744 static int prepare_data(int fd, int revents, void *cb_data)
745 {
746         struct sr_dev_inst *sdi;
747         struct dev_context *devc;
748         struct sr_datafeed_packet packet;
749         struct sr_datafeed_logic logic;
750         struct analog_gen *ag;
751         GHashTableIter iter;
752         void *value;
753         uint64_t logic_todo, analog_todo, expected_samplenum, analog_sent, sending_now;
754         int64_t time, elapsed;
755
756         (void)fd;
757         (void)revents;
758
759         sdi = cb_data;
760         devc = sdi->priv;
761         logic_todo = analog_todo = 0;
762
763         /* How many samples should we have sent by now? */
764         time = g_get_monotonic_time();
765         elapsed = time - devc->starttime;
766         expected_samplenum = elapsed * devc->cur_samplerate / (1000 * 1000);
767
768         /* But never more than the limit, if there is one. */
769         if (!devc->continuous)
770                 expected_samplenum = MIN(expected_samplenum, devc->limit_samples);
771
772         /* Of those, how many do we still have to send? */
773         if (devc->num_logic_channels && (devc->logic_counter < devc->limit_samples))
774                 logic_todo = expected_samplenum - devc->logic_counter;
775         if (devc->num_analog_channels && (devc->analog_counter < devc->limit_samples))
776                 analog_todo = expected_samplenum - devc->analog_counter;
777
778         while (logic_todo || analog_todo) {
779                 /* Logic */
780                 if (logic_todo > 0) {
781                         sending_now = MIN(logic_todo, LOGIC_BUFSIZE / devc->logic_unitsize);
782                         logic_generator(sdi, sending_now * devc->logic_unitsize);
783                         packet.type = SR_DF_LOGIC;
784                         packet.payload = &logic;
785                         logic.length = sending_now * devc->logic_unitsize;
786                         logic.unitsize = devc->logic_unitsize;
787                         logic.data = devc->logic_data;
788                         sr_session_send(sdi, &packet);
789                         logic_todo -= sending_now;
790                         devc->logic_counter += sending_now;
791                 }
792
793                 /* Analog, one channel at a time */
794                 if (analog_todo > 0) {
795                         analog_sent = 0;
796
797                         g_hash_table_iter_init(&iter, devc->ch_ag);
798                         while (g_hash_table_iter_next(&iter, NULL, &value)) {
799                                 send_analog_packet(value, sdi,
800                                                    &analog_sent, analog_todo);
801                         }
802                         analog_todo -= analog_sent;
803                         devc->analog_counter += analog_sent;
804                 }
805         }
806
807         if (!devc->continuous
808                         && (!devc->num_logic_channels || devc->logic_counter >= devc->limit_samples)
809                         && (!devc->num_analog_channels || devc->analog_counter >= devc->limit_samples)) {
810                 /* If we're averaging everything - now is the time to send data */
811                 if (devc->avg_samples == 0) {
812                         g_hash_table_iter_init(&iter, devc->ch_ag);
813                         while (g_hash_table_iter_next(&iter, NULL, &value)) {
814                                 ag = value;
815                                 packet.type = SR_DF_ANALOG;
816                                 packet.payload = &ag->packet;
817                                 ag->packet.data = &ag->avg_val;
818                                 ag->packet.num_samples = 1;
819                                 sr_session_send(sdi, &packet);
820                         }
821                 }
822
823                 sr_dbg("Requested number of samples reached.");
824                 dev_acquisition_stop(sdi, cb_data);
825                 return TRUE;
826         }
827
828         return TRUE;
829 }
830
831 static int dev_acquisition_start(const struct sr_dev_inst *sdi, void *cb_data)
832 {
833         struct dev_context *devc;
834         GHashTableIter iter;
835         void *value;
836
837         (void)cb_data;
838
839         if (sdi->status != SR_ST_ACTIVE)
840                 return SR_ERR_DEV_CLOSED;
841
842         devc = sdi->priv;
843         devc->continuous = !devc->limit_samples;
844         devc->logic_counter = devc->analog_counter = 0;
845
846         g_hash_table_iter_init(&iter, devc->ch_ag);
847         while (g_hash_table_iter_next(&iter, NULL, &value))
848                 generate_analog_pattern(value, devc->cur_samplerate);
849
850         sr_session_source_add(sdi->session, -1, 0, 40, prepare_data, (void *)sdi);
851
852         /* Send header packet to the session bus. */
853         std_session_send_df_header(sdi, LOG_PREFIX);
854
855         /* We use this timestamp to decide how many more samples to send. */
856         devc->starttime = g_get_monotonic_time();
857
858         return SR_OK;
859 }
860
861 static int dev_acquisition_stop(struct sr_dev_inst *sdi, void *cb_data)
862 {
863         struct sr_datafeed_packet packet;
864
865         (void)cb_data;
866
867         sr_dbg("Stopping acquisition.");
868
869         sr_session_source_remove(sdi->session, -1);
870
871         /* Send last packet. */
872         packet.type = SR_DF_END;
873         sr_session_send(sdi, &packet);
874
875         return SR_OK;
876 }
877
878 SR_PRIV struct sr_dev_driver demo_driver_info = {
879         .name = "demo",
880         .longname = "Demo driver and pattern generator",
881         .api_version = 1,
882         .init = init,
883         .cleanup = cleanup,
884         .scan = scan,
885         .dev_list = dev_list,
886         .dev_clear = NULL,
887         .config_get = config_get,
888         .config_set = config_set,
889         .config_list = config_list,
890         .dev_open = dev_open,
891         .dev_close = dev_close,
892         .dev_acquisition_start = dev_acquisition_start,
893         .dev_acquisition_stop = dev_acquisition_stop,
894         .context = NULL,
895 };