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