]> sigrok.org Git - libsigrok.git/blob - src/hardware/demo/api.c
sr_dev_close(): Set status to SR_ST_INACTIVE.
[libsigrok.git] / src / hardware / demo / api.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, see <http://www.gnu.org/licenses/>.
21  */
22
23 #include <config.h>
24 #include <stdlib.h>
25 #include <string.h>
26 #include <math.h>
27 #include <libsigrok/libsigrok.h>
28 #include "libsigrok-internal.h"
29 #include "protocol.h"
30
31 #define DEFAULT_NUM_LOGIC_CHANNELS      8
32 #define DEFAULT_LOGIC_PATTERN           PATTERN_SIGROK
33
34 #define DEFAULT_NUM_ANALOG_CHANNELS     4
35 #define DEFAULT_ANALOG_AMPLITUDE        10
36
37 /* Note: No spaces allowed because of sigrok-cli. */
38 static const char *logic_pattern_str[] = {
39         "sigrok",
40         "random",
41         "incremental",
42         "walking-one",
43         "walking-zero",
44         "all-low",
45         "all-high",
46         "squid",
47 };
48
49 static const uint32_t drvopts[] = {
50         SR_CONF_DEMO_DEV,
51         SR_CONF_LOGIC_ANALYZER,
52         SR_CONF_OSCILLOSCOPE,
53 };
54
55 static const uint32_t scanopts[] = {
56         SR_CONF_NUM_LOGIC_CHANNELS,
57         SR_CONF_NUM_ANALOG_CHANNELS,
58 };
59
60 static const uint32_t devopts[] = {
61         SR_CONF_CONTINUOUS,
62         SR_CONF_LIMIT_SAMPLES | SR_CONF_GET | SR_CONF_SET,
63         SR_CONF_LIMIT_MSEC | SR_CONF_GET | SR_CONF_SET,
64         SR_CONF_SAMPLERATE | SR_CONF_GET | SR_CONF_SET | SR_CONF_LIST,
65         SR_CONF_AVERAGING | SR_CONF_GET | SR_CONF_SET,
66         SR_CONF_AVG_SAMPLES | SR_CONF_GET | SR_CONF_SET,
67 };
68
69 static const uint32_t devopts_cg_logic[] = {
70         SR_CONF_PATTERN_MODE | SR_CONF_GET | SR_CONF_SET | SR_CONF_LIST,
71 };
72
73 static const uint32_t devopts_cg_analog_group[] = {
74         SR_CONF_AMPLITUDE | SR_CONF_GET | SR_CONF_SET,
75 };
76
77 static const uint32_t devopts_cg_analog_channel[] = {
78         SR_CONF_PATTERN_MODE | SR_CONF_GET | SR_CONF_SET | SR_CONF_LIST,
79         SR_CONF_AMPLITUDE | SR_CONF_GET | SR_CONF_SET,
80 };
81
82 static const uint64_t samplerates[] = {
83         SR_HZ(1),
84         SR_GHZ(1),
85         SR_HZ(1),
86 };
87
88 static GSList *scan(struct sr_dev_driver *di, GSList *options)
89 {
90         struct dev_context *devc;
91         struct sr_dev_inst *sdi;
92         struct sr_channel *ch;
93         struct sr_channel_group *cg, *acg;
94         struct sr_config *src;
95         struct analog_gen *ag;
96         GSList *l;
97         int num_logic_channels, num_analog_channels, pattern, i;
98         char channel_name[16];
99
100         num_logic_channels = DEFAULT_NUM_LOGIC_CHANNELS;
101         num_analog_channels = DEFAULT_NUM_ANALOG_CHANNELS;
102         for (l = options; l; l = l->next) {
103                 src = l->data;
104                 switch (src->key) {
105                 case SR_CONF_NUM_LOGIC_CHANNELS:
106                         num_logic_channels = g_variant_get_int32(src->data);
107                         break;
108                 case SR_CONF_NUM_ANALOG_CHANNELS:
109                         num_analog_channels = g_variant_get_int32(src->data);
110                         break;
111                 }
112         }
113
114         sdi = g_malloc0(sizeof(struct sr_dev_inst));
115         sdi->status = SR_ST_INACTIVE;
116         sdi->model = g_strdup("Demo device");
117
118         devc = g_malloc0(sizeof(struct dev_context));
119         devc->cur_samplerate = SR_KHZ(200);
120         devc->num_logic_channels = num_logic_channels;
121         devc->logic_unitsize = (devc->num_logic_channels + 7) / 8;
122         devc->logic_pattern = DEFAULT_LOGIC_PATTERN;
123         devc->num_analog_channels = num_analog_channels;
124
125         if (num_logic_channels > 0) {
126                 /* Logic channels, all in one channel group. */
127                 cg = g_malloc0(sizeof(struct sr_channel_group));
128                 cg->name = g_strdup("Logic");
129                 for (i = 0; i < num_logic_channels; i++) {
130                         sprintf(channel_name, "D%d", i);
131                         ch = sr_channel_new(sdi, i, SR_CHANNEL_LOGIC, TRUE, channel_name);
132                         cg->channels = g_slist_append(cg->channels, ch);
133                 }
134                 sdi->channel_groups = g_slist_append(NULL, cg);
135         }
136
137         /* Analog channels, channel groups and pattern generators. */
138         devc->ch_ag = g_hash_table_new(g_direct_hash, g_direct_equal);
139         if (num_analog_channels > 0) {
140                 pattern = 0;
141                 /* An "Analog" channel group with all analog channels in it. */
142                 acg = g_malloc0(sizeof(struct sr_channel_group));
143                 acg->name = g_strdup("Analog");
144                 sdi->channel_groups = g_slist_append(sdi->channel_groups, acg);
145
146                 for (i = 0; i < num_analog_channels; i++) {
147                         snprintf(channel_name, 16, "A%d", i);
148                         ch = sr_channel_new(sdi, i + num_logic_channels, SR_CHANNEL_ANALOG,
149                                         TRUE, channel_name);
150                         acg->channels = g_slist_append(acg->channels, ch);
151
152                         /* Every analog channel gets its own channel group as well. */
153                         cg = g_malloc0(sizeof(struct sr_channel_group));
154                         cg->name = g_strdup(channel_name);
155                         cg->channels = g_slist_append(NULL, ch);
156                         sdi->channel_groups = g_slist_append(sdi->channel_groups, cg);
157
158                         /* Every channel gets a generator struct. */
159                         ag = g_malloc(sizeof(struct analog_gen));
160                         ag->ch = ch;
161                         ag->amplitude = DEFAULT_ANALOG_AMPLITUDE;
162                         sr_analog_init(&ag->packet, &ag->encoding, &ag->meaning, &ag->spec, 2);
163                         ag->packet.meaning->channels = cg->channels;
164                         ag->packet.meaning->mq = 0;
165                         ag->packet.meaning->mqflags = 0;
166                         ag->packet.meaning->unit = SR_UNIT_VOLT;
167                         ag->packet.data = ag->pattern_data;
168                         ag->pattern = pattern;
169                         ag->avg_val = 0.0f;
170                         ag->num_avgs = 0;
171                         g_hash_table_insert(devc->ch_ag, ch, ag);
172
173                         if (++pattern == ARRAY_SIZE(analog_pattern_str))
174                                 pattern = 0;
175                 }
176         }
177
178         sdi->priv = devc;
179
180         return std_scan_complete(di, g_slist_append(NULL, sdi));
181 }
182
183 static int dev_open(struct sr_dev_inst *sdi)
184 {
185         (void)sdi;
186
187         return SR_OK;
188 }
189
190 static int dev_close(struct sr_dev_inst *sdi)
191 {
192         (void)sdi;
193
194         return SR_OK;
195 }
196
197 static void clear_helper(void *priv)
198 {
199         struct dev_context *devc;
200         GHashTableIter iter;
201         void *value;
202
203         devc = priv;
204
205         /* Analog generators. */
206         g_hash_table_iter_init(&iter, devc->ch_ag);
207         while (g_hash_table_iter_next(&iter, NULL, &value))
208                 g_free(value);
209         g_hash_table_unref(devc->ch_ag);
210         g_free(devc);
211 }
212
213 static int dev_clear(const struct sr_dev_driver *di)
214 {
215         return std_dev_clear(di, clear_helper);
216 }
217
218 static int config_get(uint32_t key, GVariant **data, const struct sr_dev_inst *sdi,
219                 const struct sr_channel_group *cg)
220 {
221         struct dev_context *devc;
222         struct sr_channel *ch;
223         struct analog_gen *ag;
224         int pattern;
225
226         if (!sdi)
227                 return SR_ERR_ARG;
228
229         devc = sdi->priv;
230         switch (key) {
231         case SR_CONF_SAMPLERATE:
232                 *data = g_variant_new_uint64(devc->cur_samplerate);
233                 break;
234         case SR_CONF_LIMIT_SAMPLES:
235                 *data = g_variant_new_uint64(devc->limit_samples);
236                 break;
237         case SR_CONF_LIMIT_MSEC:
238                 *data = g_variant_new_uint64(devc->limit_msec);
239                 break;
240         case SR_CONF_AVERAGING:
241                 *data = g_variant_new_boolean(devc->avg);
242                 break;
243         case SR_CONF_AVG_SAMPLES:
244                 *data = g_variant_new_uint64(devc->avg_samples);
245                 break;
246         case SR_CONF_PATTERN_MODE:
247                 if (!cg)
248                         return SR_ERR_CHANNEL_GROUP;
249                 /* Any channel in the group will do. */
250                 ch = cg->channels->data;
251                 if (ch->type == SR_CHANNEL_LOGIC) {
252                         pattern = devc->logic_pattern;
253                         *data = g_variant_new_string(logic_pattern_str[pattern]);
254                 } else if (ch->type == SR_CHANNEL_ANALOG) {
255                         ag = g_hash_table_lookup(devc->ch_ag, ch);
256                         pattern = ag->pattern;
257                         *data = g_variant_new_string(analog_pattern_str[pattern]);
258                 } else
259                         return SR_ERR_BUG;
260                 break;
261         case SR_CONF_AMPLITUDE:
262                 if (!cg)
263                         return SR_ERR_CHANNEL_GROUP;
264                 /* Any channel in the group will do. */
265                 ch = cg->channels->data;
266                 if (ch->type != SR_CHANNEL_ANALOG)
267                         return SR_ERR_ARG;
268                 ag = g_hash_table_lookup(devc->ch_ag, ch);
269                 *data = g_variant_new_double(ag->amplitude);
270                 break;
271         default:
272                 return SR_ERR_NA;
273         }
274
275         return SR_OK;
276 }
277
278 static int config_set(uint32_t key, GVariant *data, const struct sr_dev_inst *sdi,
279                 const struct sr_channel_group *cg)
280 {
281         struct dev_context *devc;
282         struct analog_gen *ag;
283         struct sr_channel *ch;
284         GSList *l;
285         int logic_pattern, analog_pattern, ret;
286         unsigned int i;
287         const char *stropt;
288
289         devc = sdi->priv;
290
291         ret = SR_OK;
292         switch (key) {
293         case SR_CONF_SAMPLERATE:
294                 devc->cur_samplerate = g_variant_get_uint64(data);
295                 break;
296         case SR_CONF_LIMIT_SAMPLES:
297                 devc->limit_msec = 0;
298                 devc->limit_samples = g_variant_get_uint64(data);
299                 break;
300         case SR_CONF_LIMIT_MSEC:
301                 devc->limit_msec = g_variant_get_uint64(data);
302                 devc->limit_samples = 0;
303                 break;
304         case SR_CONF_AVERAGING:
305                 devc->avg = g_variant_get_boolean(data);
306                 sr_dbg("%s averaging", devc->avg ? "Enabling" : "Disabling");
307                 break;
308         case SR_CONF_AVG_SAMPLES:
309                 devc->avg_samples = g_variant_get_uint64(data);
310                 sr_dbg("Setting averaging rate to %" PRIu64, devc->avg_samples);
311                 break;
312         case SR_CONF_PATTERN_MODE:
313                 if (!cg)
314                         return SR_ERR_CHANNEL_GROUP;
315                 stropt = g_variant_get_string(data, NULL);
316                 logic_pattern = analog_pattern = -1;
317                 for (i = 0; i < ARRAY_SIZE(logic_pattern_str); i++) {
318                         if (!strcmp(stropt, logic_pattern_str[i])) {
319                                 logic_pattern = i;
320                                 break;
321                         }
322                 }
323                 for (i = 0; i < ARRAY_SIZE(analog_pattern_str); i++) {
324                         if (!strcmp(stropt, analog_pattern_str[i])) {
325                                 analog_pattern = i;
326                                 break;
327                         }
328                 }
329                 if (logic_pattern == -1 && analog_pattern == -1)
330                         return SR_ERR_ARG;
331                 for (l = cg->channels; l; l = l->next) {
332                         ch = l->data;
333                         if (ch->type == SR_CHANNEL_LOGIC) {
334                                 if (logic_pattern == -1)
335                                         return SR_ERR_ARG;
336                                 sr_dbg("Setting logic pattern to %s",
337                                                 logic_pattern_str[logic_pattern]);
338                                 devc->logic_pattern = logic_pattern;
339                                 /* Might as well do this now, these are static. */
340                                 if (logic_pattern == PATTERN_ALL_LOW)
341                                         memset(devc->logic_data, 0x00, LOGIC_BUFSIZE);
342                                 else if (logic_pattern == PATTERN_ALL_HIGH)
343                                         memset(devc->logic_data, 0xff, LOGIC_BUFSIZE);
344                         } else if (ch->type == SR_CHANNEL_ANALOG) {
345                                 if (analog_pattern == -1)
346                                         return SR_ERR_ARG;
347                                 sr_dbg("Setting analog pattern for channel %s to %s",
348                                                 ch->name, analog_pattern_str[analog_pattern]);
349                                 ag = g_hash_table_lookup(devc->ch_ag, ch);
350                                 ag->pattern = analog_pattern;
351                         } else
352                                 return SR_ERR_BUG;
353                 }
354                 break;
355         case SR_CONF_AMPLITUDE:
356                 if (!cg)
357                         return SR_ERR_CHANNEL_GROUP;
358                 for (l = cg->channels; l; l = l->next) {
359                         ch = l->data;
360                         if (ch->type != SR_CHANNEL_ANALOG)
361                                 return SR_ERR_ARG;
362                         ag = g_hash_table_lookup(devc->ch_ag, ch);
363                         ag->amplitude = g_variant_get_double(data);
364                 }
365                 break;
366         default:
367                 ret = SR_ERR_NA;
368         }
369
370         return ret;
371 }
372
373 static int config_list(uint32_t key, GVariant **data, const struct sr_dev_inst *sdi,
374                 const struct sr_channel_group *cg)
375 {
376         struct sr_channel *ch;
377         GVariant *gvar;
378         GVariantBuilder gvb;
379
380         if (key == SR_CONF_SCAN_OPTIONS) {
381                 *data = g_variant_new_fixed_array(G_VARIANT_TYPE_UINT32,
382                                 scanopts, ARRAY_SIZE(scanopts), sizeof(uint32_t));
383                 return SR_OK;
384         }
385
386         if (key == SR_CONF_DEVICE_OPTIONS && !sdi) {
387                 *data = g_variant_new_fixed_array(G_VARIANT_TYPE_UINT32,
388                                 drvopts, ARRAY_SIZE(drvopts), sizeof(uint32_t));
389                 return SR_OK;
390         }
391
392         if (!sdi)
393                 return SR_ERR_ARG;
394
395         if (!cg) {
396                 switch (key) {
397                 case SR_CONF_DEVICE_OPTIONS:
398                         *data = g_variant_new_fixed_array(G_VARIANT_TYPE_UINT32,
399                                         devopts, ARRAY_SIZE(devopts), sizeof(uint32_t));
400                         break;
401                 case SR_CONF_SAMPLERATE:
402                         g_variant_builder_init(&gvb, G_VARIANT_TYPE("a{sv}"));
403                         gvar = g_variant_new_fixed_array(G_VARIANT_TYPE("t"), samplerates,
404                                         ARRAY_SIZE(samplerates), sizeof(uint64_t));
405                         g_variant_builder_add(&gvb, "{sv}", "samplerate-steps", gvar);
406                         *data = g_variant_builder_end(&gvb);
407                         break;
408                 default:
409                         return SR_ERR_NA;
410                 }
411         } else {
412                 ch = cg->channels->data;
413                 switch (key) {
414                 case SR_CONF_DEVICE_OPTIONS:
415                         if (ch->type == SR_CHANNEL_LOGIC)
416                                 *data = g_variant_new_fixed_array(G_VARIANT_TYPE_UINT32,
417                                                 devopts_cg_logic, ARRAY_SIZE(devopts_cg_logic),
418                                                 sizeof(uint32_t));
419                         else if (ch->type == SR_CHANNEL_ANALOG) {
420                                 if (strcmp(cg->name, "Analog") == 0)
421                                         *data = g_variant_new_fixed_array(G_VARIANT_TYPE_UINT32,
422                                                         devopts_cg_analog_group, ARRAY_SIZE(devopts_cg_analog_group),
423                                                         sizeof(uint32_t));
424                                 else
425                                         *data = g_variant_new_fixed_array(G_VARIANT_TYPE_UINT32,
426                                                         devopts_cg_analog_channel, ARRAY_SIZE(devopts_cg_analog_channel),
427                                                         sizeof(uint32_t));
428                         }
429                         else
430                                 return SR_ERR_BUG;
431                         break;
432                 case SR_CONF_PATTERN_MODE:
433                         /* The analog group (with all 4 channels) shall not have a pattern property. */
434                         if (strcmp(cg->name, "Analog") == 0)
435                                 return SR_ERR_NA;
436
437                         if (ch->type == SR_CHANNEL_LOGIC)
438                                 *data = g_variant_new_strv(logic_pattern_str,
439                                                 ARRAY_SIZE(logic_pattern_str));
440                         else if (ch->type == SR_CHANNEL_ANALOG)
441                                 *data = g_variant_new_strv(analog_pattern_str,
442                                                 ARRAY_SIZE(analog_pattern_str));
443                         else
444                                 return SR_ERR_BUG;
445                         break;
446                 default:
447                         return SR_ERR_NA;
448                 }
449         }
450
451         return SR_OK;
452 }
453
454 static int dev_acquisition_start(const struct sr_dev_inst *sdi)
455 {
456         struct dev_context *devc;
457         GSList *l;
458         struct sr_channel *ch;
459         int bitpos;
460         uint8_t mask;
461         GHashTableIter iter;
462         void *value;
463
464         devc = sdi->priv;
465         devc->sent_samples = 0;
466
467         /*
468          * Determine the numbers of logic and analog channels that are
469          * involved in the acquisition. Determine an offset and a mask to
470          * remove excess logic data content before datafeed submission.
471          */
472         devc->enabled_logic_channels = 0;
473         devc->enabled_analog_channels = 0;
474         for (l = sdi->channels; l; l = l->next) {
475                 ch = l->data;
476                 if (!ch->enabled)
477                         continue;
478                 if (ch->type == SR_CHANNEL_ANALOG) {
479                         devc->enabled_analog_channels++;
480                         continue;
481                 }
482                 if (ch->type != SR_CHANNEL_LOGIC)
483                         continue;
484                 /*
485                  * TODO: Need we create a channel map here, such that the
486                  * session datafeed packets will have a dense representation
487                  * of the enabled channels' data? For example store channels
488                  * D3 and D5 in bit positions 0 and 1 respectively, when all
489                  * other channels are disabled? The current implementation
490                  * generates a sparse layout, might provide data for logic
491                  * channels that are disabled while it might suppress data
492                  * from enabled channels at the same time.
493                  */
494                 devc->enabled_logic_channels++;
495         }
496         devc->first_partial_logic_index = devc->enabled_logic_channels / 8;
497         bitpos = devc->enabled_logic_channels % 8;
498         mask = (1 << bitpos) - 1;
499         devc->first_partial_logic_mask = mask;
500         sr_dbg("DBG: %s(), num logic %zu, partial off %zu, mask 0x%02x",
501                 __func__, devc->enabled_logic_channels,
502                 devc->first_partial_logic_index,
503                 devc->first_partial_logic_mask);
504
505         /*
506          * Have the waveform for analog patterns pre-generated. It's
507          * supposed to be periodic, so the generator just needs to
508          * access the prepared sample data (DDS style).
509          */
510         g_hash_table_iter_init(&iter, devc->ch_ag);
511         while (g_hash_table_iter_next(&iter, NULL, &value))
512                 demo_generate_analog_pattern(value, devc->cur_samplerate);
513
514         sr_session_source_add(sdi->session, -1, 0, 100,
515                         demo_prepare_data, (struct sr_dev_inst *)sdi);
516
517         std_session_send_df_header(sdi);
518
519         /* We use this timestamp to decide how many more samples to send. */
520         devc->start_us = g_get_monotonic_time();
521         devc->spent_us = 0;
522         devc->step = 0;
523
524         return SR_OK;
525 }
526
527 static int dev_acquisition_stop(struct sr_dev_inst *sdi)
528 {
529         sr_session_source_remove(sdi->session, -1);
530         std_session_send_df_end(sdi);
531
532         return SR_OK;
533 }
534
535 static struct sr_dev_driver demo_driver_info = {
536         .name = "demo",
537         .longname = "Demo driver and pattern generator",
538         .api_version = 1,
539         .init = std_init,
540         .cleanup = std_cleanup,
541         .scan = scan,
542         .dev_list = std_dev_list,
543         .dev_clear = dev_clear,
544         .config_get = config_get,
545         .config_set = config_set,
546         .config_list = config_list,
547         .dev_open = dev_open,
548         .dev_close = dev_close,
549         .dev_acquisition_start = dev_acquisition_start,
550         .dev_acquisition_stop = dev_acquisition_stop,
551         .context = NULL,
552 };
553 SR_REGISTER_DEV_DRIVER(demo_driver_info);