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